Why must a final variable be initialized before constructor completes?

后端 未结 9 496
暖寄归人
暖寄归人 2020-12-01 03:38

Why must a final variable be initialized before constructor completes?

public class Ex
{
  final int q;
}

When I compile this code I get er

9条回答
  •  天命终不由人
    2020-12-01 04:13

    The value of a final variable can only be set once. The constructor is the only place in the code for a class that you can guarantee this will hold true; the constructor is only ever called once for an object but other methods can be called any number of times.

提交回复
热议问题