Declare final variable, but set later

后端 未结 7 1549
梦毁少年i
梦毁少年i 2020-12-09 01:49

I know this is fairly simple topic, but I really want to wrap my head around it.

This is what I\'m trying to do, but it doesn\'t like the final modifier. Is there an

7条回答
  •  执笔经年
    2020-12-09 02:22

    You can set later a global final Variable only in your constructor. Example:

    public class ClassA {
       private final long mID;
    
       public ClassA(final long mID) {
          this.mID = mID;
       }
    }
    

    In this case in each constructor you have to initialize the final variable.

提交回复
热议问题