Java design pattern: Singleton

Today we will take a look at the first Java design pattern from the category of patterns intended for creating objects (creational patterns) – Singleton .

Read more – a series of design patterns:

What is the Singleton Java design pattern?

Singleton is a design pattern that restricts the creation of instances of a class to a single instance and provides global access to that instance. In other words, the Singleton pattern ensures that there is only one instance of a given class in Java, and that instance is easily accessible from anywhere in the program.

What problem does Singleton’s design pattern solve?

Singleton design pattern is useful when you need to ensure that there is only one instance of a given class in an application. For example, you can use the Singleton pattern to ensure that there is only one instance of a database connection object, a logging object, or a configuration object in the application. This avoids wasted resources and ensures consistency in application.

Example of Singleton implementation in Java

We will now show how to implement the Singleton pattern in Java.

We’ll create a class that we’ll call Singleton.
In Singleton, we implement a private constructor that prevents direct instance creation.

Next, we’ll need a variable for the instance itself, which we’ll call instance. The variable will be of type private static and will be initialized to null.

The getInstance() method will always be called to get an instance of the singleton. We create and program this so that if the instance does not already exist, it is created on the first call to getInstance() and subsequent calls to getInstance() return the same instance.

Add a public void method showMessage with arbitrary text to the class.

package designpatterns;

public class Singleton {
    // Pretoze k tejto premennej pristupujeme zo statickej metody,
    // musi byt tiez staticka.
    private static Singleton instance = null;

    // Privatny konstruktor zabrani vytvoreniu instancie.
    private Singleton() {

    // Pre pristup k instancii budeme pouzivat tuto metodu.
    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        return instance;

    // Metoda na verifikovanie volania metody na instancii.
    public void showMessage() {
        System.out.println("You have implemented design pattern Singleton.");

Here is an example of using the Singleton class:

import designpatterns.Singleton;

public class Main {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getInstance();

This code prints the message “You have implemented design pattern Singleton.” because showMessage() is called on the Singleton instance.

We have prepared files with the aforementioned example Java Singleton code that you can run directly in Java. Download the code here.

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you