Best Practice: Java static non final variables

前端 未结 7 832
北荒
北荒 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:34

    Personally for class non-final variables I use the CamelCase notation. It is clear from code that it is a class variable since you have to reference it as such: FooBar.bDoNotRunTests.

    On that note, I prefix class instance variables with the this to distinguish them from local scope variables. ex. this.bDoNotRunTests.

    0 讨论(0)
  • 2020-12-08 15:35

    In my experience static non-final variables should only be used for singleton instances. Everything else can be either more cleanly contained by a singleton (such as a cache), or made final (such as a logger reference). However I don't believe in hard and fast rules, so I would take my advice with a grain of salt. That said I would suggest carefully examining any case where you consider declaring a non-final static variable aside from a singleton instance and see if it can be refactored or implemented differently -- i.e. moved into a singleton container or use a final reference to a mutable object.

    0 讨论(0)
  • 2020-12-08 15:41

    Static variables can be used to control application-level behaviour, for example specifying global logging level, server to connect with.

    I've met such use cases in old appliations, usually coming from other companies.

    Nowadays using static variables for such purposes is obviously bad practice, but it wasn't so obvious in, say, 1999. No Spring, no log4j, no Clean code from R.C.Martin etc.

    Java language is quite old now, and even if some feature is strongly discouraged now, it was often used in the beginnings. And because of backward compatibility it's unlikely to change.

    0 讨论(0)
  • 2020-12-08 15:50

    When used as a cache, logging, statistics or a debug switch are the obvious reasonable uses. All private, of course.

    If you have mutable object assigned to a final field, that is morally the same as having a mutable field.

    Some languages, such as Fan, completely disallow mutable statics (or equivalent).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题