Singleton Method Design Pattern in Java

Last Updated : 18 May, 2026

Singleton Design Pattern in Java ensures that a class has only one instance throughout the application and provides a global access point to it. It is mainly used when a single shared object is required, such as database connections, logging, or configuration settings. The constructor is made private to prevent creating multiple objects from outside the class.

  • Allows only one object of the class to be created
  • Provides global access to the same instance
  • Helps save memory by reusing the same object

Note: While designing a Singleton class, make the constructor private and provide a static method that returns the object of the class using the Lazy Initialization technique.

Purpose of Singleton Class

The main purpose of a Singleton class in Java is to ensure that only one object of the class is created during the entire application lifecycle. It provides a single global access point to shared resources like database connections, sockets, caches, and configuration settings.

  • Memory Efficient: The object is created only once and reused whenever needed, reducing memory usage and object creation overhead.
  • Resource Control: Helps manage shared resources efficiently in applications such as caching, logging, thread pooling, and database connectivity.
  • Thread Safety: Ensures that only one thread or one connection accesses a shared resource at a time in multi-threaded environments.

Example of singleton classes is Runtime class, Action Servlet, and Service Locator. Private constructors and factory methods are also an example of the singleton class.

Forms of Singleton Class Pattern

There are two forms of singleton design patterns, which are:

  • Early Instantiation: The object is created at the time of class loading.
  • Lazy Instantiation: The object is created only when it is required for the first time.

Creating a Singleton Class in Java

To create a Singleton class in Java, follow these steps:

  • Create a private constructor to prevent direct object creation from outside the class.
  • Declare a private static variable to store the single instance of the class.
  • Create a public static method (commonly getInstance()) that returns the same instance every time using the Lazy Initialization approach.
  • This ensures that only one object of the class is created throughout the application.

Example 1: Singleton class using the getInstance() method.

Java
class Singleton 
{
    // Static variable reference of single_instance
    // of type Singleton
    private static Singleton single_instance = null;

    // Declaring a variable of type String
    public String s;

    // Constructor
    // Here we will be creating private constructor
    // restricted to this class itself
    private Singleton()
    {
        s = "Hello I am a string part of Singleton class";
    }

    // Static method
    // Static method to create instance of Singleton class
    public static synchronized Singleton getInstance()
    {
        if (single_instance == null)
            single_instance = new Singleton();

        return single_instance;
    }
}

// Main class
class Geeks 
{
    // Main driver method
    public static void main(String args[])
    {
        // Instantiating Singleton class with variable x
        Singleton x = Singleton.getInstance();

        // Instantiating Singleton class with variable y
        Singleton y = Singleton.getInstance();

        // Instantiating Singleton class with variable z
        Singleton z = Singleton.getInstance();

        // Printing the hash code for above variable as
        // declared
        System.out.println("Hashcode of x is "
                           + x.hashCode());
        System.out.println("Hashcode of y is "
                           + y.hashCode());
        System.out.println("Hashcode of z is "
                           + z.hashCode());

        // Condition check
        if (x == y && y == z) {

            System.out.println(
                "Three objects point to the same memory location on the heap i.e, to the same object");
        }

        else {
            
            System.out.println(
                "Three objects DO NOT point to the same memory location on the heap");
        }
    }
}

Output
Hashcode of x is 1995265320
Hashcode of y is 1995265320
Hashcode of z is 1995265320
Three objects point to the same memory location on the heap i.e, to the same object

Explanation: In the above program, the getInstance() method is used to create the Singleton object. When it is called for the first time, it creates the object single_instance; after that, the same object is returned every time instead of creating a new one.

Singleton class

The variable single_instance is declared as static, so it is shared among all objects of the class. That is why objects x, y, and z have the same hashcode, which shows that all three references point to the same object in memory.

Example 2: Singleton class with the same method name as class.

Java
class Singleton 
{
    // Static variable single_instance of type Singleton
    private static Singleton single_instance = null;

    // Declaring a variable of type String
    public String s;

    // Constructor of this class
    // Here private constructor is used to
    // restricted to this class itself
    private Singleton()
    {
        s = "String from Singleton class";
    }

    // Method
    // Static method to create instance of Singleton class
    public static Singleton Singleton()
    {
        // To ensure only one instance is created
        if (single_instance == null) {
            single_instance = new Singleton();
        }
        return single_instance;
    }
}

// Main class
class Geeks
{
    // Main driver method
    public static void main(String args[])
    {
        // Instantiating Singleton class with variable x
        Singleton x = Singleton.Singleton();

        // Instantiating Singleton class with variable y
        Singleton y = Singleton.Singleton();

        // Now changing variable of instance x
        // via toUpperCase() method
        x.s = (x.s).toUpperCase();

        // Print and display commands
        System.out.println("String from x: " + x.s + ", y: " + y.s);

        // Now again changing variable of instance y
        y.s = (y.s).toLowerCase();

        System.out.println("String from x: " + x.s + ", y: " + y.s);
    }
}

Output
String from x: STRING FROM SINGLETON CLASS, y: STRING FROM SINGLETON CLASS
String from x: string from singleton class, y: string from singleton class

Explanation: In the singleton class, when we first-time call Singleton() method, it creates an object of class Singleton with the name single_instance and returns it to the variable. Since single_instance is static, it is changed from null to some object. Next time if we try to call Singleton() method, since single_instance is not null, it is returned to the variable, instead of instantiating the Singleton class again.

Normal Class vs Singleton Class

FeatureNormal ClassSingleton Class
Object CreationMultiple objects can be createdOnly one object is created
Constructor AccessUsually publicConstructor is private
Instance AccessObjects created using new keywordObject accessed using getInstance() method
Memory UsageMore memory used due to multiple objectsMemory efficient because a single object is reused
Object ReferenceEach object has a different referenceAll references point to the same object
PurposeUsed for general object creationUsed for shared resources like logging, caching, database connection
ExampleStudent s1 = new Student();Singleton s = Singleton.getInstance();
Comment
Morty Proxy This is a proxified and sanitized view of the page, visit original site.