Singleton Class and Private Constructor in Java : Free Java Tutorials

For any Java class, if we are allowed to create only one object, then such type of class is called a singleton class. For example, Runtime and ServiceLocator are singleton classes. We can implement a Singleton class using a private constructor.

The advantage of Singleton class: Memory utilization and Performance improvement

If several people have the same requirement, then it is not feasible to create a separate object for every requirement. We have to create only one object and we can reuse the same object for every similar requirement.

This way, performance and memory utilization will be improved. This is the central idea of singleton classes.

Since we can create only one object of a singleton class, we are not allowed to use new operator for instance creation. Instead, we have to use factory methods to create objects.

For example,

  1. Runtime R1 = Runtime.getRuntime();

2. Runtime R2 = Runtime.getRuntime();

………………………upto……………………………….

n. Runtime Rn = Runtime.getRuntime();

Here, getRuntime() method will always return the same instance that is created once.

Singleton-Class-Private-Constructor

 

Creating our own Singleton class

We can create our own singleton classes by using a private constructor, private static variable, and public factory method.

Approach 1:

// Demo class
class Demo
{
    // makking one private static object
    private static Demo obj = new Demo();

    // private constructor that cannot be accessed from outside this class
    private Demo
    {
    }
    
    // factory method 
    public static Demo getDemoObject()
    {
        // returning the same instance everytime this method gets called
        return obj;
    }
}

In this approach, our constructor is private. So, one cannot create an instance using new operator and call this constructor from outside of this class.

Also, we are creating only one static object and every time someone calls the factory method, we are returning the same instance of the object.

This way, we are making sure that this is a singleton class and only one object is created and reused every time there is a request for new object creation.

Runtime class is internally implemented using this approach.

Approach 2:

This approach is not too different from the first approach. In this approach, we are not creating any object until the factory method is called.

When factory method is called for the first time, we will create an object. After then, for each upcoming factory method call, we will just return the object that we previously created.

// Demo class
class Demo
{
    // makking one private static object
    private static Demo obj = null;

    // private constructor that cannot be accessed from outside this class
    private Demo
    {
    }
    
    // factory method 
    public static Demo getDemoObject()
    {
        // if object is null, then create an object
        if(obj == null)
        {
            // object creation
            obj = new Demo();
        }
        // returning the same instance everytime this method gets called
        return obj;
    }
}

In both the approaches, at any point in time, we can create only one object for Demo class. Hence, Demo class is a singleton class.

Leave a Comment

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