Singleton pattern — only one instance of a class
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Singleton is a creational pattern that controls construction so callers use one shared instance within a defined scope. The classic Java form has a private constructor, a static field, and a static accessor. For example, Configuration.getInstance() may return one process-local configuration object, although an injected configuration service usually keeps that dependency clearer. I qualify the scope because static state follows the loaded class: another class loader or JVM process can have another instance. Lazy initialization also needs correct safe publication, and one safely created object can still have unsafe mutable methods. The main trade-off is global access, which hides dependencies and shares state across callers and tests.
Singleton pattern — only one instance of a class
Direct Answer
Singleton is a creational pattern whose intent is to control construction so one shared instance is used within a defined scope and provide access to it. A classic Java version uses a private constructor and static access, so its practical scope follows class loading rather than automatically covering every JVM, process, or cluster. Use it rarely: lifecycle ownership and explicit dependency injection are usually clearer than hidden global access.
Common Mistakes & Pitfalls
- No synchronization. A plain
if (instance == null)lets concurrent threads create multiple instances. - Forgetting
volatilein double-checked locking — without it, instruction reordering can publish a partially built object. - Mutable shared state. Using a Singleton to pass data between unrelated classes is a global variable in disguise — it couples code and breaks tests.
- Hand-rolling it inside Spring. Spring already gives you singleton-scoped beans; manual
getInstance()fights the framework.
Check your understanding
Answer these without looking back at the article.
- How do you make a Singleton thread-safe?
- Why is Spring bean scope singleton by default?
- Why is the enum Singleton considered the safest in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.