I'm trying to implement Singleton
pattern for the below class. I should have only single instance of class SingletonClass
at anytime in the JVM.
Does below code satisfy the Singleton pattern? please provide your suggestions.
public class SingletonClass {
private static SingletonClass cache = null;
private static final Object LOCK = new Object();
// creates one single instance of the class
public static SingletonClass getInstance() {
if (cache == null) {
synchronized (LOCK) {
if (cache == null) {
cache = new SingletonClass();
}
}
}
return cache;
}
public static void main(String[] args) {
SingletonClass.getInstance();
}
}