The Singleton Design Pattern and how to implement it in Java

The Singleton design pattern is one of many famous design pattern used in software development.
Sometimes you need to ensure that a class in your java project, only has one single instance. The singleton pattern is suitable for this purpose. In this post, I will show you how to implement it and describe which value it offers you in your program.

For some classes in your java application, it’s important to have exactly one instance. Let’s say you have a database which only allows one connection per application. In many cases you will have to use a connection pool. But with that restriction you need to scale down to only one connection at a time. Another example could be a print spooler

There are several ways to implement a singleton, but for all of them you need to implement the following points:

  • A static field with its own type
  • A private constructor without any parameters
  • A public static method „getInstance()“ to return its only instance

Eager initialization

public class Singleton {
  private static Singleton instance = 
new Singleton();
  private Singleton(){}

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

In this example you create an instance inside your class variable. By using the „getInstance()“ method you can get the single instance. Whenever your program starts, you will have an instance of that class. This approach is called „Eager initialization“ and can be useful, if you have not many resources inside your class.

Lazy initialization

public class Singleton {
  private static Singleton instance = null
  private Singleton(){}

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

To prevent unnecessary instantiation from the beginning, we did not create an instance of the Singleton in the „instance“ field. Only if the „getInstance()“ method is called, we will first check, if we already have an instance. If that is not the case, we will create one. That simple check prevents creating unlimited instances or unintended initial instantiation. This approach is called „Lazy Initialization“ and it gives you more controll over your ressources than the „Eager Initialization“

Thread Safe Singleton

To make the usage of your Singleton not just resource friendly but alos thread safe, you could make your method synchronized. You only need to add the synchronized keyword in the declaration of your method as shown below on our example with a lazy initializization singleton


public class Singleton {
  private static Singleton instance = null
  private Singleton(){}

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

The advantage you get from having the controll over the amount of instances of an object can vary and is coupled on the usecases you have.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.