Open In App

Difference Between Singleton Pattern and Static Class in Java

Singleton Pattern belongs to Creational type pattern. As the name suggests, the creational design type deals with object creation mechanisms. Basically, to simplify this, creational pattern explains to us the creation of objects in a manner suitable to a given situation. Singleton design pattern is used when we need to ensure that only one object of a particular class is Instantiated. That single instance created is responsible to coordinate actions across the application.

Different objects trying to invoke an object instantiated as singleton.

If you look at the illustrated diagram above you will see different objects trying to invoke an object instantiated as a singleton. This single instance of the object is responsible to invoke underneath methods or events.



Guidelines for Singleton Pattern are as follows: 

Advantages:



Implementation:

Example




// Java Program to illustrate Difference Between
// Singleton Pattern vs Static Class.
 
// Here illustrating Singleton Pattern
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Helper class
class SingletonEagar {
 
    // Declaration and definition of variable occur
    // simultaneously
    //  Eager initialisation
    private static SingletonEagar instance
        = new SingletonEagar();
 
    // Private constructor of class 1
    private SingletonEagar() {}
 
    public static SingletonEagar getInstance()
    {
        return instance;
    }
}
 
// Class 2
// Helper Class
class Singleton {
 
    // Lazy  declaration and initialisation
    private static Singleton instance;
 
    // Private constructor of Class 2
    private Singleton() {}
 
    public static Singleton getInstance()
    {
 
        // Condition check
        // When instance is null
        // a new object of Singleton class is created
        if (instance == null) {
            instance = new Singleton();
        }
 
        return instance;
    }
}
 
// Class 3
// Helper class
class SingletonSynchronizedMethod {
 
    private static SingletonSynchronizedMethod instance;
 
    // Private constructor of Class 3
    private SingletonSynchronizedMethod() {}
 
    public static synchronized SingletonSynchronizedMethod
    getInstance()
    {
 
        // Condition check
        // When instance is null
        // a new object of Singleton class is created
        if (instance == null) {
            instance = new SingletonSynchronizedMethod();
        }
 
        return instance;
    }
}
 
// Class 4
// Helper class
class SingletonSynchronized {
 
    private static SingletonSynchronized instance;
 
    // Private constructor of Class 4
    private SingletonSynchronized() {}
 
    public static SingletonSynchronized getInstance()
    {
 
        // Again, condition check
        if (instance == null) {
 
            // synchronized block
            synchronized (SingletonSynchronized.class)
            {
 
                if (instance == null) {
                    instance = new SingletonSynchronized();
                }
            }
        }
        return instance;
    }
}
 
// Class 5
// Main class (SingletonExample)
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating instance in main() method of class 1
        SingletonEagar instance1
            = SingletonEagar.getInstance();
 
        // Display message only
        System.out.println(
            instance1
            + " : Singleton Eager initialisation ");
 
        // Creating instance in main() method of class 2
        Singleton instance2 = Singleton.getInstance();
 
        // Display message only
        System.out.println(
            instance2
            + " : Singleton Lazy initialisation ");
 
        // Creating instance in main() method of class 4
        SingletonSynchronized instance3
            = SingletonSynchronized.getInstance();
 
        // Display message only
        System.out.println(instance3
                           + " : Synchronized Singleton");
    }
}

Output
SingletonEagar@2f4d3709 : Singleton Eager initialisation 
Singleton@1d81eb93 : Singleton Lazy initialisation 
SingletonSynchronized@34a245ab : Synchronized Singleton

 

Static Class 

Static nested classes are nested classes that are declared static. In Java, Static classes are a convenient way of grouping classes together. Java doesn’t allow you to create top-level static classes; only nested (inner) classes. That is why a static class is also known as a static inner class or static nested class. 

Guidelines for Static class are as follows:

Advantages:

Implementation: 

Example 




// Java Program to illustrate Difference Between
// Singleton Pattern vs Static Class.
 
// Here illustrating Static Class
 
// Importing input output classes
import java.io.*;
 
// Class 1
// Helper Class
class GFG_Outer {
 
    // Custom input integer value
    static int x = 20;
 
    public static void display() { System.out.println(x); }
    // Class 2
    // Static inner class
 
    // Can access all static variables of outer class
    // Can't access non-static members
    static class GFG_Inner {
 
        GFG_Inner()
        {
 
            // Display message
            System.out.println(
                "This is Inner Class Constructor...");
 
            // Print the value of x from inside class thrown
            // on console
            System.out.println(
                "Value of \"x\" from inside Inner Class is : "
                + x);
        }
    }
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Printing value of x in the main() method
        System.out.println(GFG_Outer.x);
 
        // Calling display() method from class 1
        GFG_Outer.display();
 
        // static inner class object which is as follows:
        // OuterClass.InnerClass variable = new
        // OuterClass.InnerClass();
        GFG_Outer.GFG_Inner object
            = new GFG_Outer.GFG_Inner();
    }
}

Output
20
20
This is Inner Class Constructor...
Value of "x" from inside Inner Class is : 20

Now we have understood the concepts and working of both of them and have seen conclusive differences in both of them. Now let’s wrap it up by illustrating all differences in a tabular format which is as follows:

Singleton Pattern Static Class
Singleton is a design pattern. Static classes are basically a way of grouping classes together in Java.
Memory is allocated once the object is created. Memory is allocated immediately after any of the class members is accessed.
Singleton implementation can either have static members or instance members. Static classes can contain static members only.
It can implement any other interface or base class is required. It cannot implement the interface or any other base class.
Singleton classes can be used as a method parameter. Static class cannot be used as a method parameter. 
Singleton pattern uses Heap memory. Static classes use stack memory.
It works within the scope of Garbage Collector as it uses Heap memory. Out of scope for Garbage Collector as it uses stack memory.
It supports Dependency Injection (DI) implementation as Singleton follows OOPS concepts. It cannot implement Dependency Injection (DI) as DI is Interface-driven.
Singleton is an architectural pattern and not a language feature. Static is a language feature and not an Architectural pattern.
Disposal of objects is possible. It cannot dispose of the static class as there is no instance created.

Article Tags :