Create Singleton Class

Creating Singleton Class in Java- Tech blog
In this article, I will show how to write Singleton class in JAVA

Definition:- A class called Singleton class if we can create one and only one instance of that class.

Fact about Singleton class

  • Singleton class is mainly used to control the object creation.
  • If required, we can allow more object. But then it won’t be called as as Singleton.
  • Since there is only one object of Singleton class, any instance variable will be occurring only once just like static fields.
  • Singleton will be used to maintain the access of resource creation. Ex. database connection, network sockets etc.
  • Servlet in Java and HibernateSessionFactory are real example where Singleton is required.Servlet is by default implements singleton, while HibernateSessionFactory are configured to serve as Singleton.
Now let us do some practical.

To make any class Singleton, we need to make sure about following 3 things

  • Constructor of that class must be private
  • Create one private static variable of type that class only.
  • Create one public static method which will actually create instance of that class and assign to this static variable.

There are two flavor of Singleton class as below
  • Instantly
  • Lazy
both flavor will follow all 3 things which are must for Singleton class. The only difference between in them is the time when they will create the instance of class. 
Instantly flavor will create the instance of class as soon as it loaded in the memory while Lazy flavor will create instance when very first request will come.
1. Instantly flavor of Singleton class

Below snippet represent Instantly flavor

public class InstantlySingleTon{
 //private static instance variable 
 private static InstantlySingleTon instantlySingleTon = getInstance();
 //Private constructor
 private InstantlySingleTon(){};
 //Private static method that will create instance.
 private static InstantlySingleTon getInstance(){
  return new InstantlySingleTon();
 //public static method which return the already created instance
 public static InstantlySingleTon getInstantlySingleTon(){
  return instantlySingleTon;
You can notice that …

  • first we have created static instance variable of type same class. 
  • Second we created private constructor and private static method that will actually create the instance as soon as this class loads into memory. While creating the instance variable we are assigning its value through this private static method.
  • That is the reason, its called Instantly flavor Singleton class.
  • This flavor had tow static methods. One is private which actually create the instance while another is public which actually return the instance.

2. Lazy flavor of Singleton class

Below snippet represent the Lazy flavor of Singleton class

public class LazySingleTon{
 //private static instance variable initialize with null
 private static LazySingleTon lazySingleTon = null;
 //Private constructor
 private LazySingleTon(){};
 //public static method which return the already created instance
 public static LazySingleTon getLazySingleTon(){
  if(lazySingleTon == null){
   lazySingleTon = new LazySingleTon();
  return lazySingleTon;

You can notice that…

  • First we created private static instance variable of type same class.
  • But this time we are not creating the instance so setting it to null
  • Next we created private constructor so nobody will create instance of this class
  • At the last we created public static method which will check if the instance variable is null then it will first create it and then return it. 
  • This way, the public static method will only create the instance when we first time call it. Second and sub-sequence call will get same object.
  • That is why we are calling it Lazy because the instance will be created on first call.
You may put some logger/SOPs in between to know how its works.


Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

6 Comments to “Create Singleton Class”

  1. Also, the scope of singleton is within the JVM. In distributed environment with multiple JVMs, the singleton class can have one instance per JVM (and thus more than one instances in the application) and still it is called singleton.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.