Why singleton-pattern is not working?
Hi,
I have two classes, singleton:
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
protected Singleton() {
System.out.println("Constructor of Singleton");
}
public static Singleton getInstance() {
return INSTANCE;
}
}
And a tester:
public class Test {
public static void main(String[] args) {
Singleton s1 = new Singleton();
Singleton s2 = new Singleton();
}
}
And the output is:
Constructor of Singleton
Constructor of Singleton
Constructor of Singleton
Why? As far as I know you shouldn't even be able to compile class Test.
[717 byte] By [
mikkomeha] at [2007-9-28 10:57:26]

This is a wrong implementation of Singleton, keep your constructor private.
public class Singleton {
private static final Singleton INSTANCE = null; // Don't initialize it here, can you think of few reasons :-)
private Singleton() {
System.out.println("Constructor of Singleton");
}
public static Singleton getInstance() {
if( null == INSTANCE )
INSTANCE = new Singleton();
return INSTANCE; // Do you follow this as the naming convention for static instances ?
}
}
Sure you can compile it. You defined constructor - protected, which makes it accessible for all classes in the same package.
INSTANCE should not be a final field. Once initialized to null, a final field cannot be re-initialized.
> public class Singleton {
> private static final Singleton INSTANCE = null; //
> Don't initialize it here, can you think of few
> reasons :-)
Could you, please, explain more. I have no idea why it would be bad to initialize the INSTANCE?
Thanks,
Boris
Hi Boris,Lot of description has been given at http://www-106.ibm.com/developerworks/java/library/j-dcl.html.Please gothrough this url.Thanks,Chandra
?
this arictle explains nicely why double checked locking does not work.
It does not explain why you shouldn't use eager initialization in a singleton ....
which is a good question IMHO since, the synchronization
and check against null brings an overhead with it. A small one but still.
Of course the normal reason for lazy instantiation is to
speed up startup, and savings in resources if the singleton is never used at all, but this thread is still missing the reasons for the general "DON'T"
regards
spieler
hello
you use the signleton incorrectly,as follow:
1 the constructor should be private(in some situation,you can use the protect
key word,but that is called the incomplete singleton)
2 in your test cllient,you you should use the getInstance method to get the
instance of the singleton
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {
System.out.println("Constructor of Singleton");
}
public static Singleton getInstance() {
return INSTANCE;
}
}
public class Test {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
}
}
Wow. I just compiled your sample code and discovered (as did you, obviously) that your protected constructor can be called from within the same package context. It seems that I have been wrong about what protected access means for the past three years...
For those of you who are interested, here is a relevant bug ID that shows the conversation:
BUG ID:4133951
Anyway, change your constructor to private and you should be OK (at least until we find out that private really doesn't mean private...). I would also recommend that you do eager instantiation ( private static Singleton instance = new Singleton() ) instead of the double locking example mentioned above. Until the get around to implementing JSR 133, of course...