ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Using the Singleton Pattern
Subject:   Use static initializer for thread safety
Date:   2003-08-29 13:10:36
From:   anonymous2
public static Object getInstance(){
if(instance == null)
instance = new Object();
return instance;
}


isn't thread safe.


this is:


private static Object instance = new Object();


public static Object getInstance(){
return instance;
}


Of course you don't want your constructor to throw an exception or you'll get a ClassNotFoundException...