On the subject of Singleton patterns I felt the need to mention Bill pugh solution or the Lazy Initialization Holder Class idiom, which is a lazy-loaded singleton. I recommend to use this when the instantiation is expensive and it is not safe to do it on time of class loading.

public class Singleton {
private Singleton() {}

private static class LazyHolder {
private static final Something INSTANCE = new Singleton();
}

public static Singleton getInstance() {
return LazyHolder.INSTANCE;
}
}


It will not initialize the INSTANCE before it’s needed. But if there is a risk that the instantiation will fail you should choose another idiom.

It can be used instead of the double check lock singleton which I prefer because it’s easier to read and there is no question about when the object will be created.

public class Singleton{
private Singleton() {}

        private static SingletonINSTANCE=null;
 
public static Singleton getInstance() {
if(INSTANCE==null){
synchronize(Singleton.class){
if(INSTANCE==null)
INSTANCE=new Singleton();
}
}
return INSTANCE;
}
}