Best Practice: Java static non final variables

前端 未结 7 831
北荒
北荒 2020-12-08 15:03

In Java, when should static non final variables be used?

For example

private static int MY_VAR = 0;

Obviously we are not talking ab

7条回答
  •  隐瞒了意图╮
    2020-12-08 15:52

    A static variable means that it is available to the class as a whole so both examples are available to the class as a whole. Final means that the value cannot be changed. So I guess the question is when do you want to a value to be available to an entire class and it cannot be changed after it has been instantiated. My guess would be a constant available to all instantiations of that class. Otherwise if you need something like a population counter then the non-final variable.

提交回复
热议问题