public class Singleton {
    private static Singleton instance;

    private Singleton() {}

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

    public static void main(String[] args) {
        Singleton s = Singleton.getInstance();
        System.out.println("Hello World from Singleton!");
    }
}
Back to Design Patterns
Creational Design Pattern

Singleton

The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is useful when exactly one object is needed to coordinate actions across the system, such as managing shared resources like configuration settings, connection pools, or logging services.