Java 单例应该使用静态变量吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18634415/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:42:17  来源:igfitidea点击:

Should a Java singleton use static variables?

javasingleton

提问by Andreas

Should a singleton in Java use static variables or member variables? Are there any advantages to either?

Java 中的单例应该使用静态变量还是成员变量?两者都有什么优势吗?

采纳答案by Bathsheba

You should use member variables. A singleton is an object (i.e. an instance of a class) and so should be modelled as such; even if you only ever intend to create one of them.

您应该使用成员变量。单例是一个对象(即类的实例),因此应该这样建模;即使您只打算创建其中之一。

Statics should be used for class level variables.

静态应该用于类级别的变量。

回答by anubhava

You can avoid using static variables and use Enuminstead:

您可以避免使用静态变量,Enum而是使用:

public enum MySingleton {
    INSTANCE;
}

You can access this singleton as MySingleton.INSTANCE.

您可以作为MySingleton.INSTANCE.

Enum is thread safe and implementation of Singleton through Enum ensures that your singleton will have only one instance even in a multithreaded environment.

Enum 是线程安全的,通过 Enum 实现 Singleton 可确保即使在多线程环境中,您的单例也只有一个实例。

回答by Juned Ahsan

There is no mandate to use static variables or member variables. As singleton is going to have only one instance so logically, using static or member variable does not make any difference. Apart form that, static and instance variable difference hold, static variables will be initialized during class loading but instance variables will be initialized during instance creation.

没有强制要求使用静态变量或成员变量。由于单例逻辑上只有一个实例,因此使用静态或成员变量没有任何区别。除了静态变量和实例变量的区别,静态变量在类加载时初始化,实例变量在实例创建时初始化。

But as a general programming rule, you should decided whether you need a static variable or not. Don't simply create public static variables and end up with unnecessary problems. So in my personal opinion, instance variables should be preferred to keep things simple and in control.

但是作为一般的编程规则,您应该决定是否需要静态变量。不要简单地创建公共静态变量并最终导致不必要的问题。所以在我个人看来,应该首选实例变量来保持简单和可控。

回答by SpringLearner

Its not mandatory to use static variables.You can use enum also

使用静态变量不是强制性的。您也可以使用枚举

see this

看到这个

回答by svobol13

It depends on concrete variable. Definitely the most common usage is that singleton is normal object that holds member variables. You can, for example, easily replace one object with another (with all other properties).

这取决于具体的变量。绝对最常见的用法是单例是包含成员变量的普通对象。例如,您可以轻松地将一个对象替换为另一个(具有所有其他属性)。

Every class can have static variables but it does not deppend wheter it is singleton or not.

每个类都可以有静态变量,但这并不取决于它是否是单例。

Singleton pattern in its nature deal with instance of object. Statics are the metter of classes --> no relation with singleton pattern.

单例模式本质上处理对象的实例。静态是类的度量标准 --> 与单例模式无关。

回答by GerritCap

A lot of information is already given: use static variables or a static variable pointing to your instance or use an enum.

已经给出了很多信息:使用静态变量或指向您的实例的静态变量或使用枚举。

A big difference is when your single ton is a member of a class that is a subclass of another class.

一个很大的区别是当你的单吨是一个类的成员时,它是另一个类的子类。

Hence your singleton instance inherits from the super class.

因此,您的单例实例继承自超类。

This can have a huge advantage.

这可以有一个巨大的优势。

Enums are not allowed to extend each other, but you can use an enum that implements interfaces.

枚举不允许相互扩展,但您可以使用实现接口的枚举。

回答by Bohemian

There needs to be a static reference to the singleton instance, but the instance itself should use instance variables, just like a regular class.

需要有一个对单例实例的静态引用,但实例本身应该使用实例变量,就像普通类一样。

The reason is that the singleton instance is after all an object, so the usual good design principles still apply to its class.

原因是单例实例毕竟是一个对象,所以通常好的设计原则仍然适用于它的类。

Further, todayit's a singleton, but tomorrow it may be a ThreadLocal, or not have any kind of instance creation restrictions. The change between these architectural choices is very low if the class is designed in the usual way. If you use static fields, such changes would require more maintenance work to make the fields non-static.

此外,今天它是一个单例,但明天它可能是一个 ThreadLocal,或者没有任何类型的实例创建限制。如果以通常的方式设计类,那么这些架构选择之间的变化非常小。如果您使用静态字段,此类更改将需要更多的维护工作以使字段非静态。