SingletonPattern
Workable solutions
Non-lazy loading
public class Singleton1 {
private final static Singleton1 INSTANCE = new Singleton1();
private Singleton1() {
}
public static Singleton1 getInstance() {
return INSTANCE;
}
}
public class Singleton2 {
private final static Singleton2 INSTANCE;
static {
INSTANCE = new Singleton2();
}
private Singleton2() {
}
public static Singleton2 getInstance() {
return INSTANCE;
}
}Lazy loading
Based on synchronized keyword
Double checked locking + Volatile
Static inner class
Winner: Enumeration
Last updated
Was this helpful?