Difference Between Singleton Pattern and Static Class in Java - GeeksforGeeks

21 Feb, 2022
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:
Singleton controls concurrent access to the resource.
It ensures there is only one object available across the application in a controlled state.
Ensure that only one instance of the class exists.
Provide global instance to that access by declaring all constructors of the class to be private, providing a static method that returns a reference to the instance, and the instance is stored as a private static variable.
Example
Java
// Java Program to illustrate Difference Between// Singleton Pattern vs Static Class. // Here illustrating Singleton Pattern // Importing input output classesimport java.io.*; // Class 1// Helper classclass 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 Classclass 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 classclass 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 classclass 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");    }}
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:
Defining the related or helper classes is possible inside the class by making it static.
It can access the private member of the enclosing class through the object reference.
The static class provides a nice namespace for the nested class.
If the enclosing class gets updated, we are able to update the static class as well at the same location.
In JVM, Classloader loads the static class at the time of the first usage only, not when its enclosing class gets loaded.
Implementation: 
Static class can only be an inner class or a nested class.
Static classes can use any type of access modifier (private, protected, public, or default) like any other static member.
Static classes are able to access only the static members of their enclosing class.
Static class can interact with the non-static member only through the object of its enclosing class only, as it cannot access the non-static members of its enclosing class directly.
Example 
Java
// Java Program to illustrate Difference Between// Singleton Pattern vs Static Class. // Here illustrating Static Class // Importing input output classesimport java.io.*; // Class 1// Helper Classclass 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();    }}
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:
gulshankumarar231
kapoorsagar226
as5853535
Java-Pattern
Picked
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Prim's and Kruskal's algorithm for MST
Difference between Internal and External fragmentation
Difference between Compile-time and Run-time Polymorphism in Java
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
Arrays.sort() in Java with examples