Quick Answer
The Singleton Design Pattern in Java ensures that only one instance of a class is created during the entire application lifecycle. The same instance is shared across the application. It is commonly used for shared resources such as logger classes, configuration objects, database connections, and Spring beans, where multiple instances can lead to inconsistency or performance issues.
Introduction
If you’ve been working with Java for a while, you’ve definitely used the Singleton pattern even if you didn’t consciously realise it.
In this article, we’ll break down the Singleton Design Pattern in Java, understand why it exists, how to implement it correctly, and where it is actually used in real-world Spring Boot projects.
What Is the Singleton Design Pattern?
A Singleton ensures that only one instance of a class is created during the entire lifecycle of an application.
In simple terms:
No matter how many times you request the object, you always get the same instance.
Why Do We Need Singleton?
In a normal Java class:
- You can create multiple objects using the
newkeyword - Each object occupies separate memory
This becomes a problem when a class manages shared resources.
Without Singleton:
- Memory wastage increases
- Behaviour becomes inconsistent
- Shared resources are accessed uncontrollably
Singleton solves this by ensuring single, controlled access to such resources.
How to Create Singleton Class in Java ?
1 - Private Constructor
private Logger() {
System.out.println("Logger instance created");
}
- Prevents object creation using
new - Ensures instance creation is controlled inside the class
2- Static Instance
private static Logger logger;
- Holds the single instance of the class
- Shared across the entire application
3- Public Static Method
public static Logger createInstance() {
if (logger == null) {
logger = new Logger();
}
return logger;
}
- Creates the object only once
- Returns the same instance on every call
How Singleton Works at Runtime ?
Logger log1 = Logger.createInstance();
Logger log2 = Logger.createInstance();
- Constructor is called only once
- Both
log1andlog2refer to the same object log1 == log2returns true
This confirms that only one instance exists in memory.
Singleton in Multi-Threaded Applications
In single-threaded applications, basic Singleton works correctly.
In multi-threaded environments, multiple threads may create multiple instances at the same time.
To avoid this, synchronization or thread-safe Singleton techniques must be used.
Without synchronization, Singleton is not thread-safe.
Where Is Singleton Used in Real Projects ?
Singleton is commonly used in:
- Logger classes
- Database connection managers
- Configuration readers
- Caching mechanisms
These components require consistent behaviour and controlled access across the application.
Singleton in Spring Framework
In Spring Framework:
- Objects are created by the IoC container
- The default bean scope is singleton
- Only one instance per bean class is created
This means Spring beans follow the Singleton Design Pattern by default, which improves performance and consistency.
FAQs
What is Singleton Design Pattern in Java ?
It is a design pattern that ensures only one instance of a class exists throughout the application lifecycle.
Why is Singleton used in real projects ?
It is used to manage shared resources like loggers, database connections, and configuration objects safely.
Is Singleton thread-safe by default ?
No. Basic Singleton implementation is not thread-safe and needs synchronization in multi-threaded applications.
Where is Singleton used in Spring ?
Spring beans are singleton-scoped by default, meaning one instance is created per bean class.
Is Singleton important for Java interviews ?
Yes. Singleton is a commonly asked design pattern and is expected to be explained with real-world use cases.
Final Thoughts
Understanding why Singleton exists is more important than memorising its syntax.
Design patterns are meant to solve real architectural problems, not just answer interview questions. Start observing where Singleton already exists in your current project.