How to implement a singleton design pattern in Java

Isaac Ssemugenyi
3 min readDec 2, 2023

--

Photo by Anne Nygård on Unsplash

When it comes to software engineering, singleton is one of the design patterns available. This pattern, like many others, can be implemented in practically any programming language, including Java.

After speaking with a Senior Engineer who asked me questions about this pattern, specifically in Java, a flame was created in me to revisit it and even write about it if for any reason someone might want to take a look and possibly learn something.

But, before we get into the code, you may be wondering why the hell I need to master the singleton design pattern and what applications it has in real life. Feel at ease because all of your questions will be answered today.

1. Why do I need to learn the singleton design pattern.

i. It is best practice as a developer to understand how to construct, when to use, and why to use a singleton design pattern.

ii. If you want to employ a single global state, singleton is the way to go because it always returns the same instance if it exists.

iii. Lazy initialization, which means that an instance is only produced once and that for subsequent needs, the same instance is always returned and no new instances are created.

There are other reasons why you should learn how this pattern works, but these are the ones I could come up with.

2. Where can one use the singleton design pattern.

i. In software applications such as web applications, desktop applications and even mobile applications where you need to return the same instance of an already open application and not a second instance of the application.

ii. In web application like whatsapp that require a single instance of the web application open on a single machine.

iii. And many other use cases you will face in your development career.

To implement the singleton design pattern in java, you will need to create a class and inside the class create a class variable of the data type of that class with a private access specifier.

  1. In my case private static Singleton instance;
  2. Create a private constructor of the class, in my case

private Singleton(String name, int age){

private String name;

private int age;

}

The constructor is up to you whether it is a no args or an args constructor, for this case mine is an args constructor.

3. Now create a class method through which the rest of the class can access the constructor, but in there always check if there is no existing instance of the class with the if statement on line 12. If there is an instance, return that instance otherwise create an instance.

4. The getter are only added for me to be in position to access the parameters passed to the instance of the class.

5. Now we can safely access our singleton instance but calling the call method getInstance(“name”, “age”);

With the above code we are calling the getInstance() twice but when we check our output, we only get the first details printed and not the second details and this is because given that the instance was created at line 4, it is the one that will always be returned instead of creating a new one.

Console Output

And there you have it, the singleton design pattern in java.

As a parting shoot, by default Spring and Springboot beans are singleton, with this spring only creates and manages a single instance of a bean in a given ApplicationContext.

Looking forward to your feedback on this.

Till next time, stay safe.

--

--

Isaac Ssemugenyi
Isaac Ssemugenyi

Written by Isaac Ssemugenyi

Software Engineer with Stanbic Flyhub Uganda, Love programming with javascript, nodejs, vuejs, react, react-native and Java.

Responses (2)