Best Practice: Java static non final variables

前端 未结 7 857
北荒
北荒 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

    Statistics-gathering might use non-final variables, e.g. to count the number of instances created. On the other hand, for that sort of situation you probably want to use AtomicLong etc anyway, at which point it can be final. Alternatively if you're collecting more than one stat, you could end up with a Statistics class and a final reference to an instance of it.

    It's certainly pretty rare to have (justifiably) non-final static variables.

提交回复
热议问题