20

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

Andreas
  • 7,470
  • 10
  • 51
  • 73
  • See: http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – Moritz Petersen Sep 05 '13 at 10:54
  • You would be able to build a singleton without using at least one static variable. – Sergey Kalinichenko Sep 05 '13 at 10:55
  • 1
    @dasblinkenlight Yes, I'd need one, but the question is about all the other variables that could be either member variabels or static variables. – Andreas Sep 05 '13 at 10:55
  • @MoritzPetersen My question is: given that I'm using a singleton pattern, should I use static variables or member variables? I know that this could be implemented completely using static methods and variables, but that isn't what I'm asking about. – Andreas Sep 05 '13 at 10:57
  • 2
    Ideally, you shouldn't use singletons. If I use a singleton at all I try to make it stateless. – Peter Lawrey Sep 05 '13 at 10:59

7 Answers7

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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Member variables are parents of static variables http://www.programmerinterview.com/index.php/c-cplusplus/whats-the-difference-between-a-class-variable-and-an-instance-variable/ – SpringLearner Sep 05 '13 at 11:00
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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
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.

svobol13
  • 1,842
  • 3
  • 25
  • 40
2

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

see this

SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • Since an enum is per definition static, it just dowsn't show that it uses static variables. Otherwise an enum is a nice implementation for a singleton – Xtroce Mar 30 '16 at 09:28
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.

GerritCap
  • 1,606
  • 10
  • 9