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();
  1. Lazy Initialization with StaticInstanceHolder class:
static class InstanctHolder{
private Singleton INSTANCE = new Singleton();
}
public Singleton getInstance(){
return InstanceHolder.INSTANCE;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly