public class Item3 {
public static void main(String[] args) {
Singleton s=Singleton.Single.INSTANCE.getInstance();
Singleton s2=Singleton.Single.INSTANCE.getInstance();
System.out.printf("%b",s==s2);
}
}
class Singleton {
enum Single{
INSTANCE;
Singleton s=new Singleton();
public Singleton getInstance(){
if(s==null)
return new Singleton();
else return s;
}
}
}
Take the 2-minute tour
×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
|
|||
|
No, it is simpler to code than that:
Then you use can use And NEVER do that:
this is not thread safe! What is more, the |
|||||||||||||||||
|
Your response and other samples in Singleton_pattern
|
|||
|
This is enough; you can directly use |
|||
|