19

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

4

7 回答 7

16

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.

于 2013-09-05T10:55:22.627 回答
7

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, today it'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.

于 2013-09-05T12:34:13.433 回答
5

You can avoid using static variables and use Enum instead:

public enum MySingleton {
    INSTANCE;
}

You can access this singleton as 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.

于 2013-09-05T10:58:54.127 回答
3

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.

于 2013-09-05T10:59:58.063 回答
3

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.

于 2013-09-05T11:04:39.690 回答
2

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

see this

于 2013-09-05T11:01:10.043 回答
1

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.

于 2013-09-05T11:05:18.627 回答