最佳实践:Java 静态非最终变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/731236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Practice: Java static non final variables
提问by James Van Boxtel
In Java, when should static non final variables be used?
在 Java 中,什么时候应该使用静态非最终变量?
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?
在实践中使用它们似乎很少见。你怎么认为?
采纳答案by Jon Skeet
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 AtomicLongetc anyway, at which point it can be final. Alternatively if you're collecting more than one stat, you could end up with a Statisticsclass and a final reference to an instance of it.
统计收集可能使用非最终变量,例如计算创建的实例数。另一方面,对于那种情况,您可能AtomicLong无论如何都想使用etc,此时它可能是最终的。或者,如果您要收集多个统计数据,则最终可能会得到一个Statistics类和对其实例的最终引用。
It's certainly pretty rare to have (justifiably) non-final static variables.
拥有(合理地)非最终静态变量当然非常罕见。
回答by Tom Hawtin - tackline
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.
如果您将可变对象分配给 final 字段,这在道德上与拥有可变字段相同。
Some languages, such as Fan, completely disallow mutable statics (or equivalent).
某些语言,例如 Fan,完全不允许可变静态(或等效项)。
回答by Chris B.
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.
根据我的经验,静态非最终变量应该只用于单例实例。其他所有内容都可以更清晰地包含在单例(例如缓存)中,或者成为最终的(例如记录器引用)。然而,我不相信硬性规定,所以我会接受我的建议。也就是说,我建议仔细检查您考虑声明除单例实例之外的非最终静态变量的任何情况,并查看它是否可以重构或以不同方式实现——即移动到单例容器中或使用对可变变量的最终引用目的。
回答by Danubian Sailor
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.
如今,出于此类目的使用静态变量显然是不好的做法,但在 1999 年就不是那么明显了。没有 Spring,没有 log4j,没有来自 RCMartin 的 Clean 代码等。
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.
Java 语言现在已经很老了,即使现在强烈反对某些功能,但在开始时经常使用它。而且由于向后兼容,它不太可能改变。
回答by Daniel Sokolowski
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.
对于类非最终变量,我个人使用 CamelCase 表示法。从代码中可以清楚地看出它是一个类变量,因为您必须这样引用它:FooBar.bDoNotRunTests.
On that note, I prefix class instance variables with the thisto distinguish them from local scope variables. ex. this.bDoNotRunTests.
在那一点上,我在类实例变量前面加上 ,this以将它们与局部作用域变量区分开来。前任。this.bDoNotRunTests.
回答by Uri
I think wrapping your statics and providing access via singletons (or at a minimum via static methods) is generally a good idea, since you can better control access and avoid some race condition and synchronization issues.
我认为包装您的静态并通过单例(或至少通过静态方法)提供访问通常是一个好主意,因为您可以更好地控制访问并避免一些竞争条件和同步问题。
回答by uriDium
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.
静态变量意味着它对整个类可用,因此这两个示例对整个类都可用。Final 表示该值不能更改。所以我想问题是你什么时候想要一个值对整个类可用,并且在实例化后不能更改。我的猜测是该类的所有实例都可以使用的常量。否则,如果您需要诸如人口计数器之类的东西,那么非最终变量。

