Singleton Pattern In Java – Devstringx

Back to Blog
Banner Java Singleton Pattern

Singleton Pattern In Java – Devstringx

What is the Singleton Pattern?

Singleton Pattern is not a keyword or a method inside our code, but it is a concept that we use to make our class instantiable only once throughout the code.

In Singleton Pattern, we define a class that has only one instance and provide global point access to it.

Now, the main purpose of using Singleton is to restrict this type of declaration. We use singleton Patterns only when we want to use the class declaration only once. With the declaration itself, the object is created. And it is not changed throughout.

No matter how many times we are declaring the instance, it will always refer to the same object. We will see how it works now through the picture.

singleton class

How to Create a Singleton Pattern in Java?

To create any singleton class, we should have static variables and methods in the class, a constructor that is private, and finally a method that returns the object of the constructor.

  • Static member: It gets memory only once because of static, it contains the instance of the Singleton class.
  • Private constructor: It will prevent the user from instantiating the Singleton class from outside the class.
  • Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.

Step 1:

We need to create a static object of a class within the class.

Class Abc

{

            Static Abc obj= new Abc();

}

Step 2:

We need to make a constructor and make it private.

Class Abc

{

            private Abc()

            {  //Define all the functionality here

            }

}

Step 3:

Now, define a method that should be static and it should return an object/ instance of the class Abc.

public static Abc getInstance()

{

            return obj;

}

Example of Singleton Example

Now, coming to the main method( how to use the instance of the above class):

Public static void main(String[] args)
{
Abc obj3= new Abc(); 		//We cannot do this, as the constructor is private. 
    Abc obj1= Abc.getInstance();
    Abc obj2= Abc.getInstance(); // there is no other way to access the methods inside this class. 
}

Recommended To Read – Import Excel Data to SQLite DB Using Java

Ways of Creating a Singleton

There are two ways of creating a Singleton Pattern in our class:

  1. Eager Initialization
  2. Lazy Initialization

1) Eager Initialization

The very first line of the code itself explains the working.

As per the principle of Eager Initialization, the instance of the class should be created in advance.

The class is loaded automatically by the JVM and only one object will be created throughout the application. This will follow the Singleton Design pattern.

2) Lazy Initialization

In the lazy initialization process, the Singleton class object will only get instantiated when we call the getInstance() method. The check is done only the first time and then the object will create. If an object already exists, JVM will not create any other instance of the class, but always refer to the same object each time the getInstance method is called.

Advantages of Singleton Design Pattern in Java

The main advantage of using Singleton is that it saves memory and objects are not created each time the method is called.

It solves the problem of creating one object of one class and the object can be used wherever required. Singleton is a creational pattern.

This makes sure that only one instance of a class exists and also provides global access for this object.

Recommended To Read – The Execution Time Of Multiple Methods In Selenium Java

Use of Singleton Class In Java

There are many ways to use the Singleton class, one of the examples would be creating a connection with the Database. We can have a connection once for each DB and access it throughout the code with the same connection. Refer to the code for a brief overview.

Singleton Class

If you are interested in even more software testing-related articles and information from us here at Devstringx, then we have a lot to choose for you.

Share this post

Back to Blog