J
joe
Typical i write my singletons like this:
protected static MyClass singleton;
protected MyClass() { }
public static MyClass getInstance() {
if( singleton == null ) {
singleton = new MyClass();
}
return singleton;
}
However on more than once occasion I've come across this:
protected static MyClass singleton = new MyClass();
protected MyClass() {}
public static MyClass getInstance() {
return singleton;
}
Now for some reason the seconds way bothers me. I am not sure why ? Is
there something to this ? Or I am just being stupid ?
joe
protected static MyClass singleton;
protected MyClass() { }
public static MyClass getInstance() {
if( singleton == null ) {
singleton = new MyClass();
}
return singleton;
}
However on more than once occasion I've come across this:
protected static MyClass singleton = new MyClass();
protected MyClass() {}
public static MyClass getInstance() {
return singleton;
}
Now for some reason the seconds way bothers me. I am not sure why ? Is
there something to this ? Or I am just being stupid ?
joe