0

What is the difference between private static variable and private instance variable in a singleton class?

I see no semantic difference.

EDIT: Not asking if the variable holding the instance of itself should be static, but other data members.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

5 Answers5

1

The way I see it, there is only "no semantic difference" if you assume that the singleton is implemented using a static reference to the single instance. The thing is, static is just one way to implement a singleton — it's an implementation detail. You can implement singletons other ways, too.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Hi, in case i am not referring to the variable holding the instance of the singleton itself, but other normal data members, is there any distinction between private instance and private static? – Oh Chin Boon Jan 12 '12 at 00:59
1

There is no difference really (besides initialization blocks mentioned already). But on the other hand you are not really gaining anything also. You still need to take thread safety into consideration and you still need to make sure you have only one instance of your singleton at a time. The only difference would be if you wanted to publish that member via a public static method. But why on earth would you want to do that - I have no idea.

For me personally it would also be a bit of a "code smell". I mean someone made a singleton and still declared its member as static? What does it tell me? Is there something I don't know? Or maybe there's something wrong with the implementation and it has to be static (but why?!). But I'm a bit paranoid. From what I am also aware of there are no performance reasons why this would be a good option.

Mateusz Dymczyk
  • 14,969
  • 10
  • 59
  • 94
1

I'm not sure what you are looking for, so I'll write something and see what you have to say.

public class Elvis {
  private static final Elvis INSTANCE = new Elvis();
  private double weight; // in pounds

  private Elvis() { weight = 300.; } // inaccessible

  public static Elvis getInstance() { return INSTANCE; }

  public double getWeight() { return weight; }
  public void setWeight(double weight) { this.weight = weight; }
}

Since there is only one Elvis, you could have made weight a static variable (with static getter and setter). If you make all variables static, then there is no point in defining a singleton INSTANCE since you just have a class with only static variables and methods:

public class Elvis {
  private static double weight; // in pounds
  static { weight = 300.; } // Could just have set the weight directly above,
                // but a static block might be useful for more complex examples. 

  public static double getWeight() { return weight; }
  public static void setWeight(double weight) { this.weight = weight; }
}

I guess this should be avoided since it looks more like a header file in C than OO.

Some might have recognized the Elvis reference from J. Bloch "Effective Java". The recommendation in that book is to implement the singleton pattern with an enum with one member:

public enum Elvis {
  INSTANCE;

  private double weight = 300.;

  public double getWeight() { return weight; }  
  public void setWeight(double weight) { this.weight = weigth; }
}

Note that it looks somewhat non-standard here with the varying weight since you usually expect enum instances to be immutable.

toto2
  • 5,306
  • 21
  • 24
  • I too have read this portion of the book from Josh Bloch, whats your take, private static weight or private weight? :D – Oh Chin Boon Jan 12 '12 at 04:15
  • As I said, I don't like a class with only static member variables and functions, so I would go with non-static. See however [this stackoverflow post](http://stackoverflow.com/questions/1942903/) where most say it might not be OO to have everything static, but it's not a problem. – toto2 Jan 12 '12 at 12:56
  • So I guess it depends on what your class is actually doing. – toto2 Jan 12 '12 at 13:02
0

There are differences, though. For instance, you can't use a static block to initialize the former.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • The OP specifically mentioned "semantic differences" — clearly, there are (non?-)semantic ones, like the fact that one is `static` and the other isn't... – Matt Ball Jan 12 '12 at 00:43
  • Sure you can. `static { instance = new Singleton(); }`. – cHao Jan 12 '12 at 00:44
  • I think that by "the former" he meant instance variables. – Mateusz Dymczyk Jan 12 '12 at 00:46
  • Also, you can't use a `static` block to initialize an instance variable, but you can you a non-static initializer block. To complement cHao's comment: `{instance = new Singleton(); }`. – Matt Ball Jan 12 '12 at 00:56
-1

Probably it is better to implement it in the singleton, since that way you can override the singleton for tests and similar. Also, you keep your static state in a single place.

Luis
  • 1,294
  • 7
  • 9