When is it appropriate to use blank final variables?

前端 未结 9 1428
野趣味
野趣味 2020-12-16 00:24

I was looking at another question about final variables and noticed that you can declare final variables without initializing them (a blank final variable). Is there a reaso

9条回答
  •  孤街浪徒
    2020-12-16 01:09

    Blank final variables must be assigned "somewhere" in the constructor. A rather constructed example:

    public class Test {
        final int sign;
        public Test(String upDown) {
            if (upDown.equals("up")) {
                sign = +1;
            } else {
                sign = -1;
            }
        }
    }
    

提交回复
热议问题