Singleton class In Java

Singleton Class in Java

Singleton class in java is a class that can have only a single object. It also means it can have just one instance of a class at the same time. In java, even when we try to make another object from the singleton class. It will only point towards the earlier created instance. So any changes in the class, they will only reflect upon the single created instance of that class. The singleton class has the power to control the instantiation of the class, therefore they also have the flexibility that it can change the whole process of creating an instance.  

We need to make some modifications to make a class singleton.

  • By making the singleton class private.
  • We write a static method that can only return an object of singleton type. We can do this by delaying the creation of an object, this process is also called as lazy initialization. The process will enhance the performance as the dependency on the creation of an object is only handled when it is needed.
  • It needs to provide a global access point to get the instance of a class i.e. object.
  • We also create a private attribute that points towards the singleton object of the class.

To understand this class better, we need to understand the difference between normal class and singleton class

Normal vs Singleton class

  • For normal class, we initialize constructor but for the singleton class we use getInstance() method.
  • The singleton class can implement interfaces whereas normal classes do not.
  • Singleton classes seem to be more flexible than the normal classes.
  • We can extend singleton class but we cannot extend the normal class.

To know more about singleton class, we have singleton design patterns in Java. These patterns help us in providing best ways to create an object of the class. The patterns have a single class for the creation of an object. It makes sure that only single object gets created. We follow some approaches related to singleton design patterns such as lazy initialization, eager initialization, and thread safe singleton, enum singleton etc. It is just like a code library in java with functions used for number of coding approaches on various techniques by computer programmers.

Singleton design patterns in Java:

  • The user can create only one instance of a class throughout. Hence, it restricts the instantiation of a class again.
  • Private constructor restricts the instantiation of a class from the other classes during the process.

Java Program on Singleton Class

public class MySingleton {
private static MySingleton Obj;
static{
Obj = new MySingleton();
}
private MySingleton(){     
}
public static MySingleton getInstance(){
return Obj;
}    
public void testMe(){
System.out.println("Welcome to Developer Helps !!");
}
public static void main(String a[]){
MySingleton ms = getInstance();
ms.testMe();
}
}

The output of the following program will be:

Welcome to Developer Helps !!

Can singleton class be inherited in Java?

As we know that we cannot inherit a static class in java. However, a singleton class is inherited. It can have a base class of its own from which it can inherit the methods and functions. These classes can be serialized as well. The result of the class which is inherited by the singleton will also be a singleton. Hence, proper patterns should be followed for this. If a user wants that singleton class should not be inherited in an application, he can make the use of the β€˜sealed’ keyword. Once the class is sealed, no one can further inherit the class.

Things you should know:

If a singleton class is serializable, we can serialize the singleton object also. We can later deserialize it but it will not return the singleton object.

To resolve this thing, we can use overriding by using methods such as the readResolve() method. It will enforce the singleton class just after the object is deserialized. As a result, it will return the singleton object of the class.

Leave a comment

Your email address will not be published. Required fields are marked *