Singleton design pattern in Java

In object-oriented programming , Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the JVM (Java Virtual Machine). In other words, a class should ensure that only a single instance must be created and single object can be used by all other classes.

Usage of Singleton Pattern

When you need to ensure that there's one instance of an object available to a number of other classes, you may want to use the Singleton pattern.

Advantage

  1. Singleton Pattern can saves memory because object is not created at each request.
  2. Provide a global point of access to the object.
  3. Allow multiple instances in the future without affecting a singleton class's clients.

How to create Singleton design pattern?

In order to create a Singleton Pattern, you need the following:

  1. Static member: It gets memory only once because of static, it contains the instance of the Singleton class.
  2. Private constructor: It will prevent to instantiate the Singleton class from outside the class.
  3. Static factory method: It provides the global point of access to the Singleton object and returns the instance to the caller.
Example
class SingletonClass { private static SingletonClass sInstance = null; public String msg; private SingletonClass() { msg = "Singleton Test"; } public static SingletonClass getInstance() { if (sInstance == null) sInstance = new SingletonClass(); return sInstance; } }
public class TestClass { public static void main(String[] args) { // instantiating Singleton class with three variable obj1,obj2,obj3 SingletonClass obj1 = SingletonClass.getInstance(); SingletonClass obj2 = SingletonClass.getInstance(); SingletonClass obj3 = SingletonClass.getInstance(); // changing variable of instance obj1 obj1.msg = (obj1.msg).toUpperCase(); System.out.println("msg from obj1 is " + obj1.msg); System.out.println("msg from obj2 is " + obj2.msg); System.out.println("msg from obj3 is " + obj3.msg); System.out.println("\n"); // changing variable of instance obj3 obj3.msg = (obj3.msg).toLowerCase(); System.out.println("msg from obj1 is " + obj1.msg); System.out.println("msg from obj2 is " + obj2.msg); System.out.println("msg from obj3 is " + obj3.msg); } }
output
msg from obj1 is SINGLETON TEST msg from obj2 is SINGLETON TEST msg from obj3 is SINGLETON TEST msg from obj1 is singleton test msg from obj2 is singleton test msg from obj3 is singleton test
SingletonClass Explanation

In the SingletonClass, first time when we request first time call getInstance() method, it creates an object of the class with name sInstance and return it to the variable.

if (sInstance == null) sInstance = new SingletonClass();
Since sInstance is static, it is changed from null to some object. Second time, when we try to call getInstance() method, since sInstance is not null, then it is returned to the variable, instead of creating new object of SingletonClass. In the main method, we instantiate the singleton class with 3 objects obj1, obj2, obj3 by calling static method getInstance() . But actually after creation of object obj1, variables obj2 and obj3 are pointed to object obj1. So, if we change the variables of object obj1, that is reflected when we access the variables of objects obj2 and obj3. Also if we change the variables of object obj3, that is reflected when we access the variables of objects obj1 and obj2.

Difference between Normal class and Singleton class

The difference in normal and singleton class in terms of instantiation, That is, for a normal class we use constructor to instantiate, whereas for singleton class we use getInstance() method.

Difference between static class and singleton pattern

  1. Singleton objects are stored in Heap, but static objects are stored in stack.
  2. We can clone the singleton object, but we cannot clone the static class object .
  3. Singleton classes follow the OOP concepts), static classes do not.
  4. You can create one instance of the object and reuse it whereas you cannot create the instance of static class.