---
title: "JAVA : Singleton Pattern"  
description: "Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides"  
author: "Tarun Kumar"  
published: 2015-07-02  
updated: 2018-02-24  
canonical: https://www.mindstick.com/blog/10912/java-singleton-pattern  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# JAVA : Singleton Pattern

Previously we have seen how to create Dynamic Objective Test Application through Swing in JAVA : [Creating Dynamic Objective Test Application through Swing in JAVA ( Using MySQL Database )](https://www.mindstick.com/Articles/1800/creating-dynamic-objective-test-application-through-swing-in-java-using-mysql-database) \

**The Singleton's purpose is to control** object [creation](https://yourviews.mindstick.com/view/85506/what-was-the-causes-of-creation-of-pakistan), limiting the number of objects to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to [resources](https://yourviews.mindstick.com/view/293/renovation-causes-wastage-of-resources) such as [database connections](https://www.mindstick.com/forum/160212/explain-dependency-injection-for-database-connections-in-dot-net-core-api) or sockets.

[Singleton pattern](https://www.mindstick.com/forum/158344/how-do-you-implement-a-singleton-pattern-in-javascript) is one of the simplest [design patterns](https://www.mindstick.com/articles/337478/the-role-of-design-patterns-in-enhancing-code-reusability) in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

*[ Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and file systems are prototypical examples. Typically, those types of objects—[known as](https://answers.mindstick.com/qa/35703/ricky-ponting-is-also-known-as-what) singletons—are accessed by disparate objects throughout a [software](https://www.mindstick.com/articles/311636/best-freelancing-websites-to-get-software-development-services) system, and therefore require a global point of access. ]*

**[Singleton Design](https://www.mindstick.com/forum/33927/how-to-implement-singleton-design-pattern-in-c-sharp) Pattern Diagram:**

![JAVA : Singleton Pattern](https://www.mindstick.com/blogs/73e18926-ce30-40d1-add2-32d9586ebee2/images/81292bc3-3093-441a-a5c5-56d7f644c548.png)

The Singleton design pattern addresses all of these concerns. With the Singleton design pattern you can:

- Ensure that only one instance of a class is created
- Provide a global point of access to the object
- Allow multiple instances in the future without affecting a singleton class's clients

## \

## Singleton Pattern: Code

```
public class ClassicSingleton {
   private static ClassicSingleton instance = null;
   protected ClassicSingleton() {
      // Exists only to defeat instantiation.
   }
   public static ClassicSingleton getInstance() {
      if(instance == null) {
         instance = new ClassicSingleton();
      }
      return instance;
   }
}
```

The ClassicSingleton class maintains a static reference to the one singleton instance and returns that reference from the static getInstance() method.

ClassicSingleton employs a technique known as lazy instantiation to create the singleton; as a result, the singleton instance is not created until the getInstance() method is called for the first time. This technique ensures that singleton instances are created only when needed.

Notice that ClassicSingleton implements a protected constructor so clients cannot instantiate ClassicSingleton instances;

For completeness, here is how a client can access the Singleton:

```
//access the singleton
Singleton singleton = Singleton.getInstance();
//use the singleton
```

\

##### Watch Out for the Downsides:

An important consideration is multi-threading. If two threads call the getInstance() method at the same time, you can end up with two singletons. Making the getInstance() method synchronized solves this issue, but then you have the performance cost - calling a synchronized version of getInstance() will be slower than the non-synchronized version. However, maybe the best way around this is to sacrifice lazy-loading in the Singleton, ensuring there can only ever be on the instance.

Next, we will learn about: JAVA - [Factory Pattern](https://www.mindstick.com/blog/10913/java-factory-pattern)

---

Original Source: https://www.mindstick.com/blog/10912/java-singleton-pattern

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
