Singleton Class in Java
- Titash Roy
- Jan 26, 2022
- 3 min read
Introduction In Java, a singleton class is a special type of class that can have only one object at a time. These classes are different from the normal classes, and we use them to serve some particular requirements and to simplify the use and to remove the bottlenecks that we face during development. In this article, we will learn what a singleton class is and how we can implement a singleton class in Java.
What is a Singleton Class? Singleton Class derives its name from Singleton Design Pattern in Java. So lets understand in brief what a Singleton Pattern is. Singleton Pattern says that "define a class that has only one instance and provide a global point of access to it". After first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to any variable inside the class through any instance, it affects the variable of the single instance created and is visible if we access that variable through any variable of that class type defined.
How to create a Singleton Class? To design a singleton class, we need to do the following things:
Declare the constructor of the Singleton class as private. We declare it as private so that no other classes can instantiate or make objects from it.
A private static variable of the same class that is the only instance of the class.
Declare a static method with the return type as an object of this singleton class. Here, the concept of Lazy initialization is used to write this static method.
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
private Singleton()
{
s = "Singleton class";
}
// Static method
// Static method to create instance of Singleton class
public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton();
return single_instance;
}
}
Difference between Normal Classes and Singleton Classes We can distinguish a Singleton class from the usual classes by how the object of the class in instantiated.
To instantiate a normal class, we use a java constructor. On the other hand, to instantiate a singleton class, we use the getInstance() method.
The other difference is that a normal class vanishes at the end of the lifecycle of the application while the singleton class does not destroy with the completion of an application.
There are two forms of singleton design pattern, which are:
Early Instantiation: The object creation takes place at the load time.
Lazy Instantiation: The object creation is done according to the requirement.

Eager/Early insatantiation The most basic and simple technique to create a Singleton class is the Eager Initialization method. With this approach, the object or the instance of the class is created when JVM loads memory to the object. We accomplish this by directly assigning the reference to the object. We can use this technique when we know that the program is always going to use the object/instance of this class. Also, we use this technique when the cost of creating the instance is not too high in terms of resources and time. Below is an example.
public class EagerInitialization
{
//Instance will be created at load time
private static EagerInitialization obj = new EarlyInitialization();
public String string;
//Creating private constructor
private EagerInitialization()
{
string = "Early instantiation";
}
//Declaring static method
public static EagerInitialization getInstance()
{
return obj;
}
}
Lazy Instantiation As we know that using the Eager Initialization method to create a singleton class may lead to unnecessary creation of an object, whether the application is using it or not. So, to overcome this problem, there is another technique to create a singleton class i.e., Lazy Initialization method. The Lazy Initialization method delays the instantiation of the class until it is needed. In other words, the object is created only if it is required. This method helps in avoiding unnecessary creation of the class instance. Below is an example.
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
private Singleton()
{
s = "Lazy instantiation";
}
// Static method
// Static method to create instance of Singleton class
public static Singleton getInstance()
{
if (single_instance == null)
single_instance = new Singleton();
return single_instance;
}
}
Summary In this post, we have seen what is a singleton class and how to create it and the different ways to create it. In the upcoming blogs, we will see, how it can be used with threads.
Comments