Report this

What is the reason for this report?

Java Singleton Design Pattern Best Practices with Examples

Updated on November 5, 2022
Java Singleton Design Pattern Best Practices with Examples

Introduction

Java Singleton Pattern is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category. From the definition, it seems to be a straightforward design pattern, but when it comes to implementation, it comes with a lot of concerns.

In this article, we will learn about singleton design pattern principles, explore different ways to implement the singleton design pattern, and some of the best practices for its usage.

Singleton Pattern Principles

  • Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java Virtual Machine.
  • The singleton class must provide a global access point to get the instance of the class.
  • Singleton pattern is used for logging, drivers objects, caching, and thread pool.
  • Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade, etc.
  • Singleton design pattern is used in core Java classes also (for example, java.lang.Runtime, java.awt.Desktop).

Java Singleton Pattern Implementation

To implement a singleton pattern, we have different approaches, but all of them have the following common concepts.

  • Private constructor to restrict instantiation of the class from other classes.
  • Private static variable of the same class that is the only instance of the class.
  • Public static method that returns the instance of the class, this is the global access point for the outer world to get the instance of the singleton class.

In further sections, we will learn different approaches to singleton pattern implementation and design concerns with the implementation.

1. Eager initialization

In eager initialization, the instance of the singleton class is created at the time of class loading. The drawback to eager initialization is that the method is created even though the client application might not be using it. Here is the implementation of the static initialization singleton class:

package com.journaldev.singleton;

public class EagerInitializedSingleton {

    private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();

    // private constructor to avoid client applications using the constructor
    private EagerInitializedSingleton(){}

    public static EagerInitializedSingleton getInstance() {
        return instance;
    }
}

If your singleton class is not using a lot of resources, this is the approach to use. But in most of the scenarios, singleton classes are created for resources such as File System, Database connections, etc. We should avoid the instantiation unless the client calls the getInstance method. Also, this method doesn’t provide any options for exception handling.

2. Static block initialization

Static block initialization implementation is similar to eager initialization, except that instance of the class is created in the static block that provides the option for exception handling.

package com.journaldev.singleton;

public class StaticBlockSingleton {

    private static StaticBlockSingleton instance;

    private StaticBlockSingleton(){}

    // static block initialization for exception handling
    static {
        try {
            instance = new StaticBlockSingleton();
        } catch (Exception e) {
            throw new RuntimeException("Exception occurred in creating singleton instance");
        }
    }

    public static StaticBlockSingleton getInstance() {
        return instance;
    }
}

Both eager initialization and static block initialization create the instance even before it’s being used and that is not the best practice to use.

3. Lazy Initialization

Lazy initialization method to implement the singleton pattern creates the instance in the global access method. Here is the sample code for creating the singleton class with this approach:

package com.journaldev.singleton;

public class LazyInitializedSingleton {

    private static LazyInitializedSingleton instance;

    private LazyInitializedSingleton(){}

    public static LazyInitializedSingleton getInstance() {
        if (instance == null) {
            instance = new LazyInitializedSingleton();
        }
        return instance;
    }
}

The preceding implementation works fine in the case of the single-threaded environment, but when it comes to multi-threaded systems, it can cause issues if multiple threads are inside the if condition at the same time. It will destroy the singleton pattern and both threads will get different instances of the singleton class. In the next section, we will see different ways to create a thread-safe singleton class.

4. Thread Safe Singleton

A simple way to create a thread-safe singleton class is to make the global access method synchronized so that only one thread can execute this method at a time. Here is a general implementation of this approach:

package com.journaldev.singleton;

public class ThreadSafeSingleton {

    private static ThreadSafeSingleton instance;

    private ThreadSafeSingleton(){}

    public static synchronized ThreadSafeSingleton getInstance() {
        if (instance == null) {
            instance = new ThreadSafeSingleton();
        }
        return instance;
    }

}

The preceding implementation works fine and provides thread-safety, but it reduces the performance because of the cost associated with the synchronized method, although we need it only for the first few threads that might create separate instances. To avoid this extra overhead every time, double-checked locking principle is used. In this approach, the synchronized block is used inside the if condition with an additional check to ensure that only one instance of a singleton class is created. The following code snippet provides the double-checked locking implementation:

public static ThreadSafeSingleton getInstanceUsingDoubleLocking() {
    if (instance == null) {
        synchronized (ThreadSafeSingleton.class) {
            if (instance == null) {
                instance = new ThreadSafeSingleton();
            }
        }
    }
    return instance;
}

Continue your learning with Thread Safe Singleton Class.

5. Bill Pugh Singleton Implementation

Prior to Java 5, the Java memory model had a lot of issues, and the previous approaches used to fail in certain scenarios where too many threads tried to get the instance of the singleton class simultaneously. So Bill Pugh came up with a different approach to create the singleton class using an inner static helper class. Here is an example of the Bill Pugh Singleton implementation:

package com.journaldev.singleton;

public class BillPughSingleton {

    private BillPughSingleton(){}

    private static class SingletonHelper {
        private static final BillPughSingleton INSTANCE = new BillPughSingleton();
    }

    public static BillPughSingleton getInstance() {
        return SingletonHelper.INSTANCE;
    }
}

Notice the private inner static class that contains the instance of the singleton class. When the singleton class is loaded, SingletonHelper class is not loaded into memory and only when someone calls the getInstance() method, this class gets loaded and creates the singleton class instance. This is the most widely used approach for the singleton class as it doesn’t require synchronization.

6. Using Reflection to destroy Singleton Pattern

Reflection can be used to destroy all the previous singleton implementation approaches. Here is an example class:

package com.journaldev.singleton;

import java.lang.reflect.Constructor;

public class ReflectionSingletonTest {

    public static void main(String[] args) {
        EagerInitializedSingleton instanceOne = EagerInitializedSingleton.getInstance();
        EagerInitializedSingleton instanceTwo = null;
        try {
            Constructor[] constructors = EagerInitializedSingleton.class.getDeclaredConstructors();
            for (Constructor constructor : constructors) {
                // This code will destroy the singleton pattern
                constructor.setAccessible(true);
                instanceTwo = (EagerInitializedSingleton) constructor.newInstance();
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(instanceOne.hashCode());
        System.out.println(instanceTwo.hashCode());
    }

}

When you run the preceding test class, you will notice that hashCode of both instances is not the same which destroys the singleton pattern. Reflection is very powerful and used in a lot of frameworks like Spring and Hibernate. Continue your learning with Java Reflection Tutorial.

7. Enum Singleton

To overcome this situation with Reflection, Joshua Bloch suggests the use of enum to implement the singleton design pattern as Java ensures that any enum value is instantiated only once in a Java program. Since Java Enum values are globally accessible, so is the singleton. The drawback is that the enum type is somewhat inflexible (for example, it does not allow lazy initialization).

package com.journaldev.singleton;

public enum EnumSingleton {

    INSTANCE;

    public static void doSomething() {
        // do something
    }
}

8. Serialization and Singleton

Sometimes in distributed systems, we need to implement Serializable interface in the singleton class so that we can store its state in the file system and retrieve it at a later point in time. Here is a small singleton class that implements Serializable interface also:

package com.journaldev.singleton;

import java.io.Serializable;

public class SerializedSingleton implements Serializable {

    private static final long serialVersionUID = -7604766932017737115L;

    private SerializedSingleton(){}

    private static class SingletonHelper {
        private static final SerializedSingleton instance = new SerializedSingleton();
    }

    public static SerializedSingleton getInstance() {
        return SingletonHelper.instance;
    }

}

The problem with serialized singleton class is that whenever we deserialize it, it will create a new instance of the class. Here is an example:

package com.journaldev.singleton;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class SingletonSerializedTest {

    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        SerializedSingleton instanceOne = SerializedSingleton.getInstance();
        ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
                "filename.ser"));
        out.writeObject(instanceOne);
        out.close();

        // deserialize from file to object
        ObjectInput in = new ObjectInputStream(new FileInputStream(
                "filename.ser"));
        SerializedSingleton instanceTwo = (SerializedSingleton) in.readObject();
        in.close();

        System.out.println("instanceOne hashCode="+instanceOne.hashCode());
        System.out.println("instanceTwo hashCode="+instanceTwo.hashCode());

    }

}

That code produces this output:

Output
instanceOne hashCode=2011117821 instanceTwo hashCode=109647522

So it destroys the singleton pattern. To overcome this scenario, all we need to do is provide the implementation of readResolve() method.

protected Object readResolve() {
    return getInstance();
}

After this, you will notice that hashCode of both instances is the same in the test program.

Read about Java Serialization and Java Deserialization.

Conclusion

This article covered the singleton design pattern.

Continue your learning with more Java tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Still looking for an answer?

Was this helpful?

owesome post…evrything is discussed about singleton.keep it up…nd thanks

- poonam

Very nice post:-) keep up the good work

- erick

nice tutorial thanks … i have one question fro you How annotations works internally?

- Tarun Soni

Sir, As per u provide contains which one is the best practices for the Singleton Pattern. Regards Vishnu

- Vishnu

This web site useful for me, Thanks a ton.

- Vishnu

Hi Pankaj, Can we create more than one object of a singleton class using serialization ? Please provide your view in detail about in which condition Singleton classes can have more than one objects. Thanks in advance. Regards, H Singh

- H Singh

Very good site for design patterns. Explanation is easy and up to the mark. Thanks.

- Prakash

This is a nice article and almost everything is covered. Thanks a ton.

- Anshul Jain

Very nice tutorial. It help me lot to understand the singleton. Could you please help me how protected Object readResolve() method make i class a singleton.

- DHARMENDRA KUMAR SAHU

Very nice, explained in simple terms.

- Rajesh

Hi Pankaj, Really nice explanation. Could you please let us know the other scinarios also where singleton is not a singleton. like, cloning, how distributed systems creates only one object where multiple clusters were involved, etc… Thanks in advance,…

- Mohan

Can you please explain the Singleton enum prevents lazy loading ?I feel that the instance creation happens lazily when it is first accessed.

- Prabhavathi

Is Reflection breaks Bill Pugh Singleton Implementation? I tried it does not create new instance, it throws IllegalArgumentException But using serialization it breaks Bill Pugh Singleton Implementation. To avoid this we must use readResolve() method. Is it right? if i did mistake please mention it…

- Velmurugan A

Awesome Work buddy !!!

- Likhit

If you are using an enum as a singleton (only one enum constant “INSTANCE”), it is lazy loaded. If there are multiple enum constants, then they are all created at first access of any constant.

- Jim Halvorsen

All the articles are very supporting. thanks

- Neha

Awesome post Pankaj…This post is really very helpful…Thank you

- Sumedh

Very nice post…It was vry helpfull

- Amasa

Really Nice that Sharing your Knowledge to e-learners

- RAJ9992

Hi Pankaj, it is very nice post…one of the best among all intenet post on singleton

- Pavan

Thanks for your wonderful article

- micheal

Hi Pankaj, your articles are very help to build concepts. Could you please explain more about this statement of Bill Pugh approach: “This is the most widely used approach for Singleton class as it doesn’t require synchronization.” Why this approach doesn’t require synchronization?

- Akhil

This is the best one stop post for singleton pattern i’ve come across.I had to give a training and this post gave me everything to discuss. Thanks a ton Pankaj for your amazing work.

- Priyanka

This tutorial gave me good clarity on Singleton Pattern. Thank you.

- Vinodini Vikram

Awesome explanation and very clear thanks a ton to pankaj

- Shashi

Great!!!

- Zaid

Hi Pankaj, Thanks for the elaborate explanations. I have a suggestion for you: Can you please add a scenario whereby cloning of the Singleton object is explained with an example. It would definitely help to understand different situations where Singleton can be used. Best Regards. Thanks, Nishant Kshirsagar

- Nishant Kshirsagar

@Anuj Agarwal declaring INSTANCE as a final object restricts re – assignment of a new instance to it. final Singleton INSTANCE = new Singleton(); INSTANCE = new Singleton(); // this won’t work

- Adyasha Nayak

its nice tutorial …

- tadveer Verma

Hi pankaj, This is a nice article and almost everything is covered. it help me lot to understand singleton

- kush singh

in Singleton Class implements Serilizable, I added readResolve() method but still serialized object and deserialzed objects hashcodes are different…Please confirm me

- Prashant Shahapur

nice post…everything is discussed here…

- MohanV

The synchronized block of your code creating multiple objects in multi threaded programming

- Naga

if i call clone() method on Singleton object was it create new object or return existing one if i load a class, using classloaders, is Singleton class has same behavior? Could please explain on above…

- lokesh

Great Article Buddy… Thanks !!

- Manav

Thanks for your blog, i have a doubt. actually not clear with, where and how to use below: can you please explain with example? protected Object readResolve() { return getInstance(); }

- Sridhar

AWESOME

- sumit

Great article

- Preeti

Joshua Bloch also suggested , in case of SerializedSingleton we need to make all instance variables transient along with implemention of readResolve() method

- Abhijeet Kedare

its good

- Ramakrisha

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.