singleton java implementation code example
Example 1: java singleton implementation
public class SingletonClass {
private static final SingletonClass SINGLE_INSTANCE = new SingletonClass();
private SingletonClass() {}
public static SingletonClass getInstance() {
return SINGLE_INSTANCE;
}
}
Example 2: java singleton
public class SingletonOnDemand {
private SingletonOnDemand () {}
private static class Singleton {
private static final SingletonOnDemand instance = new SingletonOnDemand();
}
public static SingletonOnDemand getInstance () {
System.out.println("create instance");
return Singleton.instance;
}
}