Singleton pattern is a design solution which application to have only one instance of any class in Java Virtual Machine.

Singleton Patterns are used where we need only one object which can be accessed by all other classes and share them across application.

Why Singleton Pattern?

To restrict one single instance of a class and which is common point for all other classes in whole application

How to implement Singleton Pattern?

The implementation involves a static member in the Singleton class, a private constructor and a static public method that returns a reference to the static member.

getInstance() method is used provide a common access for other classes which can be accessed as below:

Approaches of Singleton Pattern implementation and design

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 provided in above example.

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 the client application might not be using it.

Here is the implementation of static initialization singleton class.

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 and we should avoid the instantiation until unless client calls the getInstance method. Also, this method doesn’t provide any options for exception handling.

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.

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

Static block initialization

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

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 Singleton class that supports lazy initialization.

We hope you understand it better. Happy learning!!!!

2 Thoughts on “Singleton Design Pattern in Java”

  • Hi,
    Can u please tell the general example for Singleton pattern.
    I mean i m going to develop a school mgmt application so how i will decide which pattern is best for my application.

    Thanks in advance!!!!

    • Thanks for reading an article.

      Best example for single pattern is Session Factory Object, Through out the application you only want single instance of your POJO classes.

      Design pattern will not form a framework, Its an easy solution given for commonly occurred problems in day to day problems. So yes in your application you can use any design pattern based on your required.

Leave a Reply

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