Singleton class in Java
What is Singleton class in Java?
Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class Abc. We can have one instance as
Abc o1 = new Abc() ; // one instance of class Abc
We can have another instance to class Abc also.
Abc o2 = new Abc() ; // instance 2 of class Abc
Here we have created two instances. That means o1 will have different values and o2 will have different values. But what if you want to create only one instance of your class? That means we are not allowed to create instance 2 ( o2 ). This means our class is Singleton class. Allowing only one instance to a class is known as Singleton class.
How to create a Singleton class?
For creating a Singleton class in Java, we have to remember a few steps.
Step 1: We have to create a static instance or a static object for the class.
class Abc
{
static Abc o1 = new Abc () ; // creating static object
}
Here in class only we are creating its instance or object.
Step 2: Don't allow the user to create an instance with the default constructor. That defines a constructor which is private.
public class Singleton
{
public static void main(String args[])
{
Abc o1 = new Abc () ; // calling the default constructor which is public
}
}
class Abc
{
static Abc o = new Abc () ;
private Abc()
{
}
}
Here new Abc () is a default constructor which is by default public. But if we create a private constructor within the class, we cannot call the default constructor.
Step 3: We should create a method that should be static which will return the object of or instance of class Abc.
public class Singleton
{
public static void main(String args[])
{
Abc o1 = new Abc () ; // calling the default constructor which is public
}
}
class Abc
{
static Abc o = new Abc () ;
private Abc()
{
}
public static Abc getObject()
{
return o;
}
}
Here the “ lazy instantiation “ concept is used to write the static method. Lazy instantiation is nothing, but the object creation is done according to the requirement.
That’s it if you follow these three steps, that means you are allowing only one instance of a class.
Now, how to get an instance of a class? Since this getObject() method is a static method, we can simply say
Abc o1 = Abc.getObject () ;
At any condition or in any case, we are allowed to create only one instance of the class Abc.
public class Singleton
{
public static void main(String args[])
{
Abc o1 = Abc.getObject();
}
}
class Abc
{
public static Abc o = new Abc() ;
private Abc()
{
}
public static Abc getObject()
{
return o;
}
}
Output

Now since object o is static. This static object will be created and will be on memory when the class is loaded so it becomes a global variable. Even if we are not using this object, it will be there in the memory. That is one of the drawbacks. If this object consumes lots of resources and if this object is bulky, it is a waste of our memory. So, this type of object creation in the Singleton class is known as early instantiation. Because if we are not using it, it will be created.
Singleton class using Lazy Instantiation
We can create the instance of a class at the time of calling the static method.
SingletonByLazy.java
public class SingletonByLazy
{
public static void main(String args[])
{
Abc o = Abc.getObject();
}
}
class Abc
{
static Abc o; // declaring the static object
private Abc()
{
}
public static Abc getObject()
{
o = new Abc(); // lazy instantiation
return o;
}
}
Output

When we say getObject() it will create a new object and it will return the object. That means if we create another object say o1, that means we are calling the getObject() method twice. So, it will create two objects. Here we lost our singleton class step 1. So, what we can do is we can just verify before creating the object. We need to verify that this is the first time we are calling our object.
SingletonByLazy1.java
public class SingletonByLazy1
{
public static void main(String args[])
{
Abc o = Abc.getObject();
}
}
class Abc
{
static Abc o; // declaring the static object
private Abc()
{
}
public static Abc getObject()
{
if ( o == null ) // verifying whether object creation is null
{
o = new Abc(); // lazy instantiation
}
return o;
}
}
Output
