The Startup
Published in

The Startup

Play Around Singleton Class in Java

We have heard the word “singleton” many time while working in java or being interviewed for the position in java development.

What is singleton class ?, Why is it being used ?, How to create singleton class in Java ? etc. These are the most common question being asked or comes to our mind while talking about singleton classes in java.

Thus this article is based on singleton class in Java and deep diving in this concept. We’ll move step by step. Lets get started.

What is a singleton class ?

In OOP, a singleton class is a class that can have only one instance or can be instantiated once at a time. So basically, we will have a class which will be called singleton if every time we instantiate the class we’ll be getting same instance.

So now we know what is singleton class and the purpose behind it, We can now play around some java code.

Creating a Singleton class is super easy. We just need to declare our constructor as private. This is only because we don’t want our class object to be instantiated directly from outside as it may led to create new instance and the very purpose of singleton will get failed.

After creating our constructor private, we will be requiring a static method which will be returning the instance when required. The singleton property is maintained, it will be taken care of by this method only. It will check if instance is already present or not and then return the same instance every time. Keeping this method as static has a reason as we will be accessing this method directly by class name.

Have a look at code snippet below.

Now its time to see our code in action. Lets create different reference variable and assign the instance of our singleton class into them.

So here we can see that all the three instances are same. Thus we have created our singleton class. Now every time we need the instance of “MySingleTonClass” we will get the same instance to work on.

But WAIT, Is it Thread safe ???

Lets test it by creating some threads and accessing “getInstance” method of our recently created singleton class.

First we will create our thread class. Look at code below.

Now access our getInstance method of MySingleTon class from the threads created.

Now lets observe the instances each thread is using.

See the instance of thread 5, 1. They are having different instance of our singleton class.

So the answer is “NO”. This implementation is not thread safe. So now we will create our thread safe singleton class.

Creating our thread safe singleton class:

For creating a thread safe singleton class, we need to make sure that our method which is responsible for creating instance is thread safe. So thanks to Java, we have synchronized & volatile keywords with us.

synchronized: This keyword is used to make resource as thread safe. When a synchronized resource is accessed by a thread, it will acquire lock and no other thread is allowed to access it.

Thus in our case we have declared our “getInstance” method as synchronized thus every time a new thread access this method to get instance of our singleton class it will not be able to access until some other thread is acquiring the lock.

volatile: A volatile variable value is always access from JVM memory. Thus if different threads are accessing this variable all will be having same value.

For more to dive into thread safety, there is lot to look into. As of now, lets have a look at code snippet below for thread safe singleton class.

Now lets test our thread safe singleton class.

Here we go, Now we have same instance for each thread. Thus we have made our singleton class thread safe.

We have another way to create our class as thread safe. Creating complete method synchronized will be an overhead if there are too many threads accessing this method as most of the time threads will have to wait. So better to opt for “synchronized block”.

We will be declaring our section of code responsible for creating instance as synchronized block. A code snippet is shown below.

So that’s it ! This is all about singleton class, creating it, its thread safety etc.

Thanks for reading :-)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store