我是一名Java程序员,刚进入企业界。最近我用Groovy和Java开发了一个应用程序。在我编写的所有代码中,都使用了相当多的静态元素。高级技术人员要求我减少使用的静态数据的数量。我在谷歌上搜索过相同的内容,我发现许多程序员都相当反对使用静态变量。

我发现静态变量使用起来更方便。而且我假定它们也是有效的(如果我错了请纠正我),因为如果我必须在一个类中对一个函数进行10,000次调用,我将很高兴使方法静态,并在其上使用直接的class . methodcall(),而不是用10,000个类实例使内存混乱,对吗?

此外,静态减少了对代码其他部分的相互依赖。他们可以作为完美的国家持有者。除此之外,我还发现一些语言(如Smalltalk和Scala)广泛地实现了静态。那么为什么程序员普遍反对静态(尤其是在Java世界中)呢?

PS:如果我对静态数据的假设是错误的,请纠正我。


当前回答

Static variables are generally considered bad because they represent global state and are therefore much more difficult to reason about. In particular, they break the assumptions of object-oriented programming. In object-oriented programming, each object has its own state, represented by instance (non-static) variables. Static variables represent state across instances which can be much more difficult to unit test. This is mainly because it is more difficult to isolate changes to static variables to a single test.

也就是说,区分常规静态变量(通常被认为是不好的)和最终静态变量(AKA常量;还不错)。

其他回答

总结在Java中使用静态方法的几个基本优点和缺点:

优点:

全局可访问,即不与任何特定的对象实例绑定。 每个JVM一个实例。 可以通过类名访问(不需要对象)。 包含一个适用于所有实例的值。 在JVM启动时加载,并在JVM关闭时死亡。 它们不会修改Object的状态。

缺点:

Static members are always part of memory whether they are in use or not. You can not control creation and destruction of static variable. Usefully they have been created at program loading and destroyed when program unload (or when JVM shuts down). You can make statics thread safe using synchronize but you need some extra efforts. If one thread change value of a static variable that can possibly break functionality of other threads. You must know “static“ before using it. You cannot override static methods. Serialization doesn't work well with them. They don't participate in runtime polymorphism. There is a memory issue (to some extent but not much I guess) if a large number of static variables/methods are used. Because they will not be Garbage Collected until program ends. Static methods are hard to test too.

因为没有人*提到它:并发。如果有多个线程读取和写入静态变量,那么静态变量可能会让您感到惊讶。这在web应用程序(例如ASP.NET)中很常见,它会导致一些相当令人恼火的错误。例如,如果有一个静态变量由页面更新,并且页面由两个人“几乎同时”请求,一个用户可能会得到另一个用户所期望的结果,甚至更糟。

静态减少了对代码其他部分的相互依赖。他们可以作为完美的国家持有者

我希望您已经准备好使用锁和处理争用。

*事实上,Preet Sangha提到过。

还有一个原因:脆弱性。

如果你有一个类,大多数人都希望能够随意创建和使用它。

您可以记录这种情况,或者防止这种情况发生(单例/工厂模式)——但这是额外的工作,因此需要额外的成本。 即使这样,在大公司中,也有可能有人在某些时候尝试使用您的类,而没有完全注意到所有好的评论或工厂。

如果你经常使用静态变量,它就会崩溃。虫子是昂贵的。

在0.0001%的性能改进和可能不了解的开发人员更改的健壮性之间,在很多情况下健壮性是不错的选择。

“静态是邪恶的”这个问题更多的是关于全局状态的问题。一个变量保持静态的适当时间是当它从来没有超过一个状态时;IE工具应该可以被整个框架访问,并且总是为相同的方法调用返回相同的结果,这些都不是静态的“邪恶”。关于你的评论:

我发现静态变量使用起来更方便。我认为他们也很有效率

对于永远不会改变的变量/类,静态是理想和有效的选择。

The problem with global state is the inherent inconsistency that it can create. Documentation about unit tests often address this issue, since any time there is a global state that can be accessed by more than multiple unrelated objects, your unit tests will be incomplete, and not 'unit' grained. As mentioned in this article about global state and singletons, if object A and B are unrelated (as in one is not expressly given reference to another), then A should not be able to affect the state of B.

在良好的代码中,有一些禁用全局状态的例外,例如时钟。时间是全局的,在某种意义上,它改变了对象的状态,而不需要编码关系。

有人可能会建议,在使用静态变量的大多数情况下,您确实希望使用单例模式。

全局状态的问题在于,有时在简单的上下文中作为全局是有意义的,但在实际的上下文中需要更加灵活,这就是单例模式变得有用的地方。