Thread-safe lazy initialization using holder class Bill Pugh Singleton implementation

suggest change
public class Singleton {
    private static class InstanceHolder {
        static final Singleton INSTANCE = new Singleton();
    }

    public static Singleton getInstance() {
        return InstanceHolder.INSTANCE;
    }

    private Singleton() {}
}

This initializes the INSTANCE variable on the first call to Singleton.getInstance(), taking advantage of the languageā€™s thread safety guarantees for static initialization without requiring additional synchronization.

This implementation is also known as Bill Pugh singleton pattern.[Wiki]

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents