Singleton class In Java

Singleton Class in Java

Singleton class in java is a class that can have only a single object. It also means it can have just one instance of a class at the same time. In java, even when we try to make another object from the singleton class. It will only point towards the earlier created instance. So any changes in the class, they will only reflect upon the single created instance of that class. The singleton class has the power to control the instantiation of the class, therefore they also have the flexibility that it can change the whole process of creating an instance.

In Java, a Singleton class is one that enables access to it through a single instance at a time. This design approach limits unwanted class instantiation and ensures that only one class object exists at any specified instant per JVM instance. As a result of this design, any class specified as Singleton has a single instance with a global point of access to it. We cannot delete a singleton class at the end of the application’s life cycle. Let’s see what is singleton class in java .

We need to make some modifications to make a class singleton.

  • By making the singleton class private.
  • We write a static method that can only return an object of singleton type. We can do this by delaying the creation of an object, this process is also called as lazy initialization. The process will enhance the performance as the dependency on the creation of an object is only handled when it is needed.
  • It needs to provide a global access point to get the instance of a class i.e. object.
  • We also create a private attribute that points towards the singleton object of the class.

To understand this class better, we need to understand the difference between normal class and singleton class

Normal vs Singleton class

  • For normal class, we initialize constructor but for the singleton class we use getInstance() method.
  • The singleton class can implement interfaces whereas normal classes do not.
  • Singleton classes seem to be more flexible than the normal classes.
  • We can extend singleton class but we cannot extend the normal class.

To know more about singleton class, we have singleton design patterns in Java. These patterns help us in providing best ways to create an object of the class. The patterns have a single class for the creation of an object. It makes sure that only single object gets created. We follow some approaches related to singleton design patterns such as lazy initialization, eager initialization, and thread safe singleton, enum singleton etc. It is just like a code library in java with functions used for number of coding approaches on various techniques by computer programmers.

Singleton design patterns in Java:

  • The user can create only one instance of a class throughout. Hence, it restricts the instantiation of a class again.
  • Private constructor restricts the instantiation of a class from the other classes during the process.

Purpose of Singleton Class

The primary purpose of Single class is to restrict the limit of the number of object creation to only one. This often ensures that there is access control to resources, for example, socket or database connection.

The memory space wastage does not occur with the use of singleton class because it restricts the instance creation. As the object creation will take place only once instead of creating it each time a new request is made.

We can use this single object repeatedly as per the requirements. This is the reason why the multi-threaded and database applications mostly make use of the Singleton pattern in Java for caching, logging, thread pooling, configuration settings and much more.

For example, there is a license with us, and we have only one database connection or suppose if our JDBC driver does not allow us to do multithreading, then Singleton class comes into the picture and makes sure that at a time, only a single connection or a single thread can access the connection

How to create singleton class in java ?

Before jumping to the method of how to make a singleton class in java. We need to keep in mind a few points:

  1. We need to ensure that there is only one class instance.
  2. We need to provide global access to instances via
  3. Declaring all the class’s constructors to be private.
  4. Including a static method that returns an instance reference.
  5. The instance which is stored as a private static variable.   

Now let’s move to the method of how to make a singleton class in java.

To make a singleton class, we require mainly three things:

  • A static member of the class
  • Private constructor
  • A static factory method

Syntax :

class developerhelps{

    // private field that refers to the object

    private static developer object;

    private developer() {

    // constructor of the codingninjas class


    public static developer getInstance() {

        // write code that allows you to create only one object

        // access the object as per the need



Singleton class in java example :

class Database {
    private static Database dbObject;

    private Database() {


    // Thread-safe initialization of the Singleton instance
    public static synchronized Database getInstance() {
        if (dbObject == null) {
            dbObject = new Database();
        return dbObject;

    public void getConnection() {

public class Main {
    public static void main(String[] args) {
        // Creating a reference to the only object of Database
        Database db1 = Database.getInstance();

Output :


Example 2 :

public class MySingleton {
private static MySingleton Obj;
Obj = new MySingleton();
private MySingleton(){     
public static MySingleton getInstance(){
return Obj;
public void testMe(){
System.out.println("Welcome to Developer Helps !!");
public static void main(String a[]){
MySingleton ms = getInstance();

Output :

Welcome to Developer Helps !!

Can singleton class be inherited in Java?

As we know we cannot inherit a static class in Java. However, a singleton class is inherited. It can have a base class of its own from which it can inherit the methods and functions. These classes can be serialized as well. The result of the class which is inherited by the singleton will also be a singleton. Hence, proper patterns should be followed for this. If a user wants that singleton class not to be inherited in an application, he can make use of the β€˜sealed’ keyword. Once the class is sealed, no one can further inherit the class.

Things you should know:

If a singleton class is serializable, we can serialize the singleton object also. We can later deserialize it but it will not return the singleton object.

To resolve this thing, we can use overriding by using methods such as the readResolve() method. It will enforce the singleton class just after the object is deserialized. As a result, it will return the singleton object of the class.

Leave a comment

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

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!