Singleton Flashcards
1
Q
Ways of creating Singleton Object
A
1. Method level synchronization: public synchronized Singleton getInstance(){ } 2. Double locking with Volatile : public Singleton getInstance(){ if(instance == null){ synchronized(singleton.class){ if(instance == null) singleton = new Singleton()
}
3. Early Initialization : private static Singleton INSTANCE = new Singleton();
- Lazy Initialization with StaticInstanceHolder class:
static class InstanctHolder{ private Singleton INSTANCE = new Singleton(); } public Singleton getInstance(){ return InstanceHolder.INSTANCE; }