How to Create Singleton Class in Java
In this tutorial, we will discuss the singleton class and how to create it.
Introduction
Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one of the best classes that exist in Java programming language. In this object-oriented programming language, Singleton is class, which means it can have only one object at a particular time or instance. After creating an instance of the class, other new variables can also point to that instance. Suppose we do any modifications with the new variables inside the class through any instance. In that case, it affects created instance and is visible if we access that new variable through any other variable of that class type defined.
Here are the few key points that must follow while creating or designing the singleton class
1. Ensure that a constructor is private.
2. Develop a static method which consists of a return type object of the singleton class. To develop this static method, we must know about lazy initialization.
Generally, there are two forms involved in the singleton design pattern,
- Early Instantiation: the instance of the class (object) takes place at load time.
- Lazy Instantiation: the creation of an instance of the class or object is done based on the requirements
Purpose of Singleton Class
The fundamental role of a Singleton class is to confine the restriction of the quantity of item creation to only one. This frequently guarantees access control to resources, for instance, database association.
The memory space wastage doesn't happen with the utilization of the singleton class since it confines the case creation. The instance of the class creation will occur just a single time instead of making it each time another solicitation is made.
We can utilize this single item over and over according to the necessities. This is why multi-threaded and database applications generally use the Singleton design in Java for reserving, logging, thread pooling, arrangement settings, and more.
For instance, there is a permit with us. We have just a single database association or assume on the off chance that our JDBC driver doesn't permit us to do multithreading, then, at that point, Singleton class comes into the image and ensures that at a time, only a solitary connection or a single thread can get to the association.
How to Design/Create a Singleton Class in Java?
To make a singleton class, we should follow the below key points:
1. Guarantee that only one instance of the class exists.
2. Give worldwide admittance to that instance by
• declaring all constructors of the class to be private.
• Giving a static method or strategy that returns a reference to the case. The apathetic statement idea is utilized to compose the static design.
• The instance is put away as a private static variable.
Illustration of singleton classes is Runtime class, Action Servlet, and Service Locator. Confidential constructors and manufacturing plant techniques are additionally an illustration of the singleton class.
The contrast between Normal Class and Singleton Class
We can recognize a Singleton class from the typical classes for the method involved with starting up the object of the class. To start up a regular class, we utilize a java constructor. Then again, to launch a singleton class, we use the getInstance() strategy.
The other contrast is that a regular class disappears toward the finish of the lifecycle of the application while the singleton class doesn't obliterate the fulfilment of an application.
Execution:
Let us brief how the singleton class changes from the typical class in Java. Here the thing that matters is as far as launch concerning the regular type, we use constructor, while for singleton class, we use getInstance() technique which we will look out for in model 1 as portrayed underneath. As a rule, to avoid disarray, we may likewise utilize the class name as a strategy name while characterizing this technique, which will be displayed in example.
Advantages
- Singleton controls concurrent access to the resource.
- It ensures only one object is available across the application in a steady state.
Example of Singleton Class:
Let's create a singleton class.
SingletonEg.java
class Test
{
public static void main(String s[])
{
// instantiating Singleton class with variable a
SingletonEg a = SingletonEg.getInstance();
// instantiating Singleton class with variable b
SingletonEg b = SingletonEg.getInstance();
// changing variable of instance a
a.str = (a.str).toUpperCase();
System.out.println("String from a is " + a.str);
System.out.println("String from b is " + b.str);
}
}
Output:
IT IS AN EXAMPLE OF SINGLETON CLASS.
IT IS AN EXAMPLE OF SINGLETON CLASS.