Java Singleton Pattern

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 very simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns.

The implementation of Java Singleton pattern has always been a controversial topic among developers. Here we will learn about Singleton design pattern principles, different ways to implement the Singleton design pattern and some of the best practices for its usage.

Singleton Pattern
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 outer world to get the instance of the singleton class.
In further sections, we will learn different approaches of Singleton pattern implementation and design concerns with the implementation.

Eager initialization
Static block initialization
Lazy Initialization
Thread Safe Singleton
Bill Pugh Singleton Implementation
Using Reflection to destroy Singleton Pattern
Enum Singleton
Serialization and Singleton

1. Eager initialization
In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class but it has a drawback that instance is created even though 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 to use 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 until unless 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 class is created in the static block that provides option for exception handling.

package com.journaldev.singleton;

public class StaticBlockSingleton {

private static StaticBlockSingleton instance;

private StaticBlockSingleton(){}

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

public static StaticBlockSingleton getInstance(){
return instance;
Both eager initialization and static block initialization creates the instance even before it’s being used and that is not the best practice to use. So in further sections, we will learn how to create a Singleton class that supports lazy initialization.

Read: Java static

3. Lazy Initialization
Lazy initialization method to implement Singleton pattern creates the instance in the global access method. Here is the sample code for creating 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 above implementation works fine in case of the single-threaded environment but when it comes to multithreaded 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 the different instances of the singleton class. In next section, we will see different ways to create a thread-safe singleton class.

4. Thread Safe Singleton
The easier 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. General implementation of this approach is like the below class.

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;

Above 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 who might create the separate instances (Read: Java Synchronization). 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;
Read: Thread Safe Singleton Class

5. Bill Pugh Singleton Implementation
Prior to Java 5, java memory model had a lot of issues and the above approaches used to fail in certain scenarios where too many threads try 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. The Bill Pugh Singleton implementation goes like this;

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 Singleton class as it doesn’t require synchronization. I am using this approach in many of my projects and it’s easy to understand and implement also.

Read: Java Nested Classes

6. Using Reflection to destroy Singleton Pattern
Reflection can be used to destroy all the above singleton implementation approaches. Let’s see this with 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) {
//Below code will destroy the singleton pattern
instanceTwo = (EagerInitializedSingleton) constructor.newInstance();
} catch (Exception e) {

When you run the above test class, you will notice that hashCode of both the instances is not same that destroys the singleton pattern. Reflection is very powerful and used in a lot of frameworks like Spring and Hibernate, do check out Java Reflection Tutorial.

7. Enum Singleton
To overcome this situation with Reflection, Joshua Bloch suggests the use of Enum to implement 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 {


public static void doSomething(){
//do something
Read: Java Enum

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

package com.journaldev.singleton;


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. Let’s see it with a simple program.

package com.journaldev.singleton;


public class SingletonSerializedTest {

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

//deserailize from file to object
ObjectInput in = new ObjectInputStream(new FileInputStream(
SerializedSingleton instanceTwo = (SerializedSingleton) in.readObject();

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


The output of the above program is;

instanceOne hashCode=2011117821
instanceTwo hashCode=109647522
So it destroys the singleton pattern, to overcome this scenario all we need to do it provide the implementation of readResolve() method.

protected Object readResolve() {
return getInstance();
After this, you will notice that hashCode of both the instances is same in the test program.

/* */

VSCodium: 100% Open Source Version of Microsoft VS Code

Brief: VSCodium is a clone of Microsoft’s popular Visual Studio Code editor. It’s identical to VS Code with the single biggest difference that unlike VS Code, VSCodium doesn’t track your usage data.

Microsoft’s Visual Studio Code is an excellent editor not only for web developers but also for other programmers. Due to its features, it’s considered one of the best open source code editors.

Yes, it’s one of the many open source products from Microsoft. You can easily install Visual Studio Code in Linux thanks to the ready to use binaries in the form of DEB, RPM and Snap packages.

And there is a problem which might not be an issue for a regular user but significant to an open source purist.

The ready to use binaries Microsoft provides are not open source.

Confused? Let me explain.

The source code of VS Code is open sourced under MIT license. You can access it on the GitHub. However, the installation files that Microsoft has created contain proprietary telemetry/tracking.

This tracking basically collects usage data and sends it to Microsoft to ‘help improve their products and services’. Telemetry reporting is common with software products these days. Even Ubuntu does that but with more transparency.

You can disable the telemetry in VS Code but can you trust Microsoft completely? If the answer is no, then what are your options?

You can build it from the source code and thus keep everything open source. But installing from source code is not always the prettiest option specially in today’s world when we are so used to of having binaries.

Another option is to use VSCodium!

VSCodium: 100% open source form of Visual Studio Code

VSCodium Completely Open Source version of VS Code

VSCodium is a clone of Microsoft’s Visual Studio Code. This project’s sole aim is to provide you with ready to use binaries without Microsoft’s telemetry code.

This solves the problem where you want to use VS Code without the proprietary code from Microsoft but you are not comfortable with building it from the source.

Since VSCodium is a clone of VS Code, it looks and functions exactly the same as VS Code.

Here’s a screenshot of the first run of VS Code and VSCodium side by side in Ubuntu. Can you distinguish one from another?

VScode vs VSCodium
Can you guess which is VSCode and VSCodium?

If you have not been able to distinguish between the two, look at the bottom.

Microsoft VSCode Tracking
That’s Microsoft

Apart from this and the logo of the two applications, there is no other noticeable difference.

VS Code vs VSCodium
VSCodium and VS Code in GNOME Menu

Installing VSCodium on Linux

While VSCodium is available in some distributions like Parrot OS, you’ll have to add additional repositories in other Linux distributions.

On Ubuntu and Debian based distributions, you can use the following commands to install VSCodium.

First, add the GPG key of the repository:

wget -qO - | sudo apt-key add -

And then add the repository itself:

echo 'deb vscodium main' | sudo tee --append /etc/apt/sources.list.d/vscodium.list

Now update your system and install VSCodium:

sudo apt update && sudo apt install codium

You can find the installation instructions for other distributions on its page. You should also read the instructions about migrating from VS Code to VSCodium.

What do you think of VSCodium?

Personally, I like the concept of VSCodium. To use the cliche, the project has its heart in the right place. I think, Linux distributions committed to open source may even start including it in their official repository.

What do you think? Is it worth switching to VSCodium or would you rather opt out of the telemetry and continue using VS Code?

And please, no “I use Vim” comments 😀