Reputation: 23443
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.
Upvotes: 0
Views: 1623
Reputation: 5326
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.
Upvotes: 1
Reputation: 15141
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.
Upvotes: 1
Reputation: 1294
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.
Upvotes: -1
Reputation: 36329
There are differences, though. For instance, you can't use a static block to initialize the former.
Upvotes: 0
Reputation: 359786
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.
Upvotes: 1