How to make the perfect Singleton?

Keval Patel
6 min readMar 27, 2017

Design Patterns are popular among software developers. A design pattern is a well described solution to a common software problem. The Singleton is one of the Creational Design Patterns in Java.

What is the purpose of Singleton?

The purpose of the Singleton class is to control object creation, limiting the number of objects to only one. The singleton allows only one entry point to create the new instance of the class.

Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons are often useful where you have to control the resources, such as database connections or sockets.

It seems to be a simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns. The implementation of Singleton pattern has always been a controversial topic among developers. Here, you are going to discuss how to create a Singleton class that fulfills its purpose :

Restrict the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine.

Let’s create Singleton class in java and test it in different conditions.

Create Singleton class



Keval Patel | Android Developer | Machine learner | Gopher | Open Source Contributor