Lazy-loading a Singleton Instance

less than 1 minute read

A thread-save version of lazy-loading a singleton instance. The instantiation of the singleton is delegated to the JVM which makes this approach fully thread-save – without explicit synchronization.

public class Singleton {
 
    // class-private singleton constructor
    private Singleton() {}
 
    private static class InstanceHolder {
        private static final Singleton instance = new Singleton();
    }
 
    public static Singleton getInstance() {
        return InstanceHolder.instance;
    }
}

Further reading:

Leave a Comment