在 Java 中使用静态变量和方法的好处
在 Java 中使用静态变量和方法有哪些好处?
What are all the benefits of using static variables and methods in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 Java 中使用静态变量和方法有哪些好处?
What are all the benefits of using static variables and methods in Java?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
静态变量的好处:
占用额外的内存(一个用于
每个类)
无需类的实例化
静态方法的优点:
定义不用担心意外
与实例的交互
班级
Benefits of static variables:
taking additional memory (one for
each class)
without an instantiation of the class
Benefits of static methods:
defined without fear of accidental
interaction with an instance of the
class
关于静态的使用一直存在争论。当您将变量或方法设置为静态时,它们不再是继承的主题,这使得它们不太灵活(例如单元测试的问题)。也就是说,如果静态方法不需要实例,则它们很有用。一个典型的例子是 java.lang.Math 的方法,大多数人都会同意 static 在这里就可以了。另一个用途是使用工厂方法作为“起点”,以便与库或框架进行交互,例如获取初始 JNDI 上下文或 JPA 的 EntityManager。然而,工厂不应该被过度使用,一旦你手里有东西,你就不应该再次调用工厂。工厂方法的现代替代方法是依赖注入(例如在 Spring、Guice 或 EJB 3.x 中)。静态变量通常用于“常量”(如
Math.PI
)。枚举是使用此技术在内部实现的。请注意,旧的单例模式现在被认为具有潜在的危险(例如,假设您需要引入池化来提高性能),并且如果您真的想要实现单例,那么最好的方法似乎是枚举只有一个元素的类。静态变量的其他用途包括注册表或全局属性之类的东西,但正如您可以猜测的那样,这又不是很灵活,应该避免。出于性能原因,重用“服务对象”(我不知道此类对象是否有明确定义的名称)可能会很好,通过将它们设为静态(日历、随机、格式化程序(如 DateFormat、Logger),但要小心避免线程问题。因此,方法和变量永远不应该仅仅为了为它们找到一个位置而被设置为静态。它们与 OO 原则(尤其是继承)相冲突,往往不灵活,难以重构和测试。对于真实的、不可变的常量(但是通常枚举是更好的选择)、“服务对象”或完全独立于对象的方法,使用 static 是很好的。当需要工厂时,它们可能是一种解决方案(但是,请考虑依赖注入或服务提供者接口)。尽量避免其他用途。
There is an ongoing debate about the usage of static. When you make a variable or method static, they are not longer subject of inheritance, which makes them less flexible (e.g. problems with unit tests). That said static methods are useful if they don't need an instance. A typical example are the methods of
java.lang.Math
, and most people would agree that static is here fine. Another use is to have a factory method as a "starting point" in order to interact with with a library or framework, like getting an initial JNDI context or an EntityManager for JPA. However, factories shouldn't be overused, once you have something in your hands, you shouldn't need to call factories again. A modern replacement for factory methods is dependency injection (e.g. in Spring, Guice or EJB 3.x). Static variables are usually used for "constants" (likeMath.PI
). Enums are internally implemented using this technique. Note that the old Singleton pattern is considered now potentially dangerous (e.g. imagine that you need to introduce pooling to improve performance), and if you really want to implement a Singleton, the best way seems to be a Enum class with only one element. Other uses of static variables include things like registries or global properties, but as you can guess, this is again not very flexible and should be avoided. For performance reasons it might be fine to reuse "service objects" (I don't know of there is a well-defined name for such objects) that are expensive to create by making them static (Calendar, Random, Formatters like DateFormat, Logger), but be careful to avoid threading issues.So methods and variables should be never made static just to find a place for them. They are in conflict with OO-principles (especially with inheritance), tend to be inflexible, hard to refactor and to test. The usage of static is fine for real, immutable constants (however often Enums are the better choice for this), "service objects" or totally object-independent methods. They might be a solution when factories are needed (however, consider dependency injection or Service Provider Interfaces instead). Try to avoid other usages.