← Back to Java

What is a Singleton class and how do you create one in Java?

Singleton Pattern in Java

By Nikola Pashov

JavaMidjava

Quick answer: The Singleton pattern ensures that a class has exactly one instance throughout the application's lifetime and provides a global point of access to it.

What a Strong Answer Covers

  • single instance
  • private constructor
  • static instance variable
  • static getInstance()
  • eager vs lazy initialization
  • double-checked locking with volatile
  • Bill Pugh inner class

Senior-Level Answer

The Singleton pattern ensures that a class has exactly one instance throughout the application's lifetime and provides a global point of access to it. It is one of the Gang of Four creational patterns.

There are several implementation approaches, each with different thread-safety and laziness characteristics.

**Eager initialization**: The instance is created when the class loads. Simple and thread-safe because class loading is atomic. Drawback: the instance is created even if never used. ```java public class Singleton { private static final Singleton INSTANCE = new Singleton(); private Singleton() {} public static Singleton getInstance() { return INSTANCE; } } ```

**Double-checked locking** (lazy, thread-safe): Delays creation until first use. The `volatile` keyword is essential — without it, the JVM may reorder instructions and a second thread can observe a partially initialized object. ```java private static volatile Singleton instance; public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) instance = new Singleton(); } } return instance; } ```

**Initialization-on-demand holder** (Bill Pugh): Leverages class loader guarantees. The nested static class is not loaded until `getInstance()` is called. Thread-safe without synchronization overhead. ```java private static class Holder { static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return Holder.INSTANCE; } ```

**Enum singleton** (Joshua Bloch's recommendation): Inherently serialization-safe and reflection-proof. Enum instances are guaranteed single by the JVM. ```java public enum Singleton { INSTANCE; } ```

Enums are preferred when you need guaranteed single-instance semantics with zero risk of serialization or reflection bypass.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Presents a correct thread-safe implementation and explains why naive lazy initialization without volatile or synchronization is broken.

3/3 — Strong Answer

Covers at least three implementation approaches, explains the volatile requirement in DCL, the class-loading guarantee in the holder pattern, and the enum approach's serialization safety.

Common Mistakes

  • Implementing double-checked locking without volatile on the instance field, which allows CPU/compiler instruction reordering to expose a partially constructed object.
  • Not making the constructor private, allowing external instantiation.
  • Forgetting that Singleton can be broken by serialization/deserialization or reflection unless using the enum approach.

Follow-Up Questions

  • Why is volatile necessary in the double-checked locking pattern? — Without volatile, the JVM can reorder the write to instance before the constructor completes, letting another thread see a non-null but partially initialized object.
  • How can reflection break a Singleton, and how does enum prevent it? — Constructor.setAccessible(true) can call private constructors. The JVM guarantees enum instances are created once and throws an exception if newInstance() is attempted.
  • When is Singleton considered an anti-pattern in modern architecture? — It creates hidden global state, making testing (mocking) difficult. Dependency injection frameworks manage instance scope without static globals.

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout