I have encountered two patterns to make a singleton class
Holder class
public class Singleton { private static final class Holder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton obtain() { return Holder.INSTANCE; } }
Instance variable class
public class Singleton { private static volatile Singleton sInstance; public static Singleton getInstance() { if (sInstance == null) { synchronized (LOCK) { if (sInstance == null) { sInstance = new Singleton(); } } return sInstance; } }
What is the differnce beetwen the two in regards to performance and best-practices?