Why must a final variable be initialized before constructor completes?

后端 未结 9 507
暖寄归人
暖寄归人 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:20

    Because final prevents you from modifying variables, but it has to be initialized at some point, and the constructors is the right place to do so.

    In your case, it would be called a blank final because it is not initialized when declared.

提交回复
热议问题