Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Singleton object- In static block or in getInstance(); which should be used

Below are two ways to implement a singleton. What are the advantages and disadvantages of each?

Static initialization:

class Singleton {
    private Singleton instance;
    static {
        instance = new Singleton();
    }
    public Singleton getInstance() {
        return instance;
    }
}

Lazy initialization is:

class Singleton {
    private Singleton instance;
    public Singleton getInstance(){
        if (instance == null) instance = new Singleton();
        return instance;
    }
}
like image 670
Rahul Rastogi Avatar asked Mar 19 '14 05:03

Rahul Rastogi


3 Answers

  1. Synchronized Accessor

    public class Singleton {
        private static Singleton instance;
    
        public static synchronized Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
    
    • lazy load
    • low perfomance
  2. Double Checked Locking & volatile

    public class Singleton {
        private static volatile Singleton instance;
        public static Singleton getInstance() {
            Singleton localInstance = instance;
            if (localInstance == null) {
                synchronized (Singleton.class) {
                    localInstance = instance;
                    if (localInstance == null) {
                        instance = localInstance = new Singleton();
                    }
                }
            }
            return localInstance;
        }
    }
    
    • lazy load
    • high perfomance
    • JDK should be 1,5 ++
  3. On Demand Holder idiom

    public class Singleton {
    
        public static class SingletonHolder {
            public static final Singleton HOLDER_INSTANCE = new Singleton();
        }
    
        public static Singleton getInstance() {
            return SingletonHolder.HOLDER_INSTANCE;
        }
    }
    
    • lazy load
    • high performance
    • cant be used for non static class fields
like image 200
Alexander Avatar answered Oct 16 '22 09:10

Alexander


First one is eager initialisation. eager initialization creates the instance even before it’s being used and that is not the best practice to use.

Second one is Lazy Initialization. Lazy implementation works fine incase of single threaded environment but when it comes to multithreaded systems, it can cause issues if multiple threads are inside the if loop at the same time. It will destroy the singleton pattern and both threads will get the different instances of singleton class.

Please visit: http://www.journaldev.com/1377/java-singleton-design-pattern-best-practices-with-examples#static-block-initialization for more information

like image 45
vanDer Avatar answered Oct 16 '22 09:10

vanDer


NOTE: static blocks can access only static variables defined outside static block directly. The object you want to restrict to only one instance creation should be static. Your first methods fails during compilation, second method requires you to create the object first
Follow this approach:

class Singleton
{
private static Singleton instance;
private Singleton()
{
}
public static Singleton getInstance()
{
    if(instance == null)
         instance = new Singleton();
    return instance;
}
}

Ensure no creation of the object elsewhere, so change the constructor to private

like image 1
BlackBeard Avatar answered Oct 16 '22 07:10

BlackBeard