Initialize a static final field in the constructor

后端 未结 8 568
执念已碎
执念已碎 2020-11-30 22:57
public class A 
{    
    private static final int x;

    public A() 
    {
        x = 5;
    }
}
  • final means the variable can
8条回答
  •  温柔的废话
    2020-11-30 23:31

    static means that the variable is unique on the application. final means that it should be set only once.

    If you set it in your constructor, you allow to set the variable more than once.

    Hence you should intialize it directly or propose a static method to initialize it.

提交回复
热议问题