Singleton Pattern in Java.

Before I start explaining how you can properly implement singleton pattern in Java, allow me to explain you a bit about what is singleton pattern.

Single Pattern defines that a class can have only one instance and that instance should be available to clients who wants to use it

As you can guess from the definition, singleton patten’s constraints are.

  1. A class should have one and only one instance and,
  2. An instance should be accessible to all its client.

1st point says that a class implementing Singleton should have one and only one instance and to achieve that class should need to restrict all other client classes to create an instance of it except class itself. Once we do that, second thing class must provide is to access to the created instance, so that other client can use as per their requirements.

So without further ado, let’s jump straight to the implementation of the singleton pattern.

Implementation:

We will try to implement singleton pattern for the class called, CompanyInformation. I am considering CompanyInformation class to provide all the static information about the company. Some of you may argue that we can easily create public final static variables to allow clients to use static information, but I strongly believe that static should be avoided as far as possible when you have other options like singleton design pattern.

This is how we can implement singleton pattern.

public class CompanyInformation {
private String name = "XYZ Pvt. Ltd.";
private String since = "2011-12-12";
private String ownerName = "Mr. PQR";
private int totalEmployee = 2500;
    //(Step 1) Declare Private Constructor
private CompanyInformation() {
}
    //(Step 2) Declare Private Static instance of a class
private static CompanyInformation instance;
    //(Step 3) Declare Public static method which will be used by client to get the instance of the class
public static CompanyInformation getInstance() {
//(Step 4) double null checking to ensure one and only one instance creation and faster access.
if (instance == null) {
synchronized (CompanyInformation.class) {
if (instance == null) {
//(step 5) Actualy Object Creation
instance = new CompanyInformation();
}
}
}
//(step 6) Return the created instance
return instance;
}
}

Let’s see each of the steps in detail and implement one by one.

(Step 1) Declare Private Constructor: This ensures that no other class than the class itself can create an instance of it.

(Step 2) Declare a Private Static instance of a class: This field will hold the one and only one instance which we will create very soon.

(Step 3) Declare Public static method which will be used by clients to get the instance of the class: This method would be used by clients to access the instance of CompanyInformation.

(Step 4) double null checking to ensure one and only one instance creation and faster access.: In this step, we will use double null checking on instance field to avoid multiple object creation in case of multi threading. First null value comparison is for letting JVM enter into the object creation process, once JVM is in, we synchronize the class itself, so out of many parallel threads, only one would be able to pass in. Once a thread is inside, we are again comparing field value against null, this is why to stop another waiting thread to not create the object once current thread creates it. Consider this image for better understanding.

As you can see in the image, even if one thread successfully creates object, another one which is in the queue, won’t able to enter into the object creation block as it will first check for null and field won’t be null anymore once first thread successfully creates object

(Step 5) Actual Object Creation: In this step, we will create an actual instance of the class.

(Step 6) Return the created instance: In This step, simply return the created instance of the class.

This way, we can implement SINGLETON design pattern perfectly. Please do comment if you want to discuss something regarding this. :)