简体   繁体   中英

Best Practice: Java static non final variables

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

For example

private static int MY_VAR = 0;

Obviously we are not talking about constants here.

public static final int MY_CONSTANT = 1;

In my experience I have often justified them when using a singleton, but then I end up needing to have more than one instance and cause myself great headache and re-factoring.

It seems it is rare that they should be used in practice. What do you think?

Statistics-gathering might use non-final variables, eg 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.

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).

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 -- ie moved into a singleton container or use a final reference to a mutable object.

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 RCMartin 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.

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 .

我认为包装你的静态和通过单例提供访问(或至少通过静态方法)通常是一个好主意,因为你可以更好地控制访问并避免一些竞争条件和同步问题。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM