Quick revision
The short answer
We write enum Registry { INSTANCE; ... } with the state and methods inside, for example a ConcurrentHashMap of handlers with register() and find() methods. Effective Java calls it the best approach for many cases because the JVM guarantees exactly one instance and thread-safe initialization, serialization returns the existing constant without readResolve, and reflection cannot create a second instance. Its limits are that it cannot extend another class and that it is created when the enum class initializes, not on demand.
What to remember
In this answer, we wrote an enum singleton with one constant, INSTANCE, holding a ConcurrentHashMap of handlers. The JVM creates it once during class initialization, which is thread-safe without any locking code. Serialization writes only the name and resolves it to the existing constant, and reflection cannot create enum instances. Its limits are that it cannot extend a class, it is created eagerly at class initialization, and, like any global, it hides dependencies, so in container-managed applications we usually prefer injected singletons.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We write enum Registry { INSTANCE; ... } with the state and methods inside, for example a ConcurrentHashMap of handlers with register() and find() methods. Effective Java calls it the best approach for many cases because the JVM guarantees exactly one instance and thread-safe initialization, serialization returns the existing constant without readResolve, and reflection cannot create a second instance. Its limits are that it cannot extend another class and that it is created when the enum class initializes, not on demand.
Why this question is asked
The singleton pattern, a class with exactly one instance, is one of the first patterns developers learn and one of the easiest to get subtly wrong. A naive version breaks under multiple threads, under serialization or under reflection. The enum version closes all three gaps in one line, which is why it is a favourite interview question and a well-known recommendation in Effective Java. In this answer, we will write the enum singleton, then attack it the three ways classic singletons fail, and finally look at its limits and when to prefer something else.
1A Quick Recap: What Can Go Wrong With a Classic Singleton
A classic singleton has a private constructor and a static field holding the one instance. The private constructor is supposed to stop anyone else creating instances. But there are three ways to get a second one. Two threads can both see the field as null and both create an instance, if initialization is lazy and not synchronized correctly. Deserialization creates a new object from bytes unless the class adds a readResolve() method that returns the existing instance. And reflection can call the private constructor after setAccessible(true).
Each gap has a fix, but each fix is extra code that someone can forget. The enum singleton gets all three for free because enums already have exactly the guarantees a singleton needs.
2Writing the Enum Singleton
An enum with a single constant is a singleton: the JVM creates INSTANCE once, when the enum class is initialized, and never creates another. We put the state and the methods in the enum body as we would in any class.
Notice the ConcurrentHashMap. The enum guarantees one instance created safely, but it does nothing for the state inside it. Because every thread shares INSTANCE, its mutable state must be thread-safe like any shared object, so we use a concurrent map rather than a HashMap.
3Thread-Safe Initialization Without Locks
Class initialization in Java is performed by the JVM exactly once, under a lock the JVM manages. If two threads use Registry at the same time, one runs the static initializer that creates INSTANCE, and the other waits until it is finished. Afterwards, every thread sees the fully constructed instance, because the Java memory model guarantees that the results of class initialization are visible to all threads that use the class.
So there is no double-checked locking, no volatile field and no synchronized method to get wrong. The cost is that creation is not lazy in the sense of on first method call; it happens when the class is initialized, which in practice is the first time any code touches Registry.
6 messages in order. Play them, or step through them one at a time.
The JVM's class initialization lock does the work that double-checked locking used to do by hand.
4Serialization Keeps the Singleton
With a normal singleton class, deserializing an instance creates a brand-new object, a second singleton, unless we add readResolve(). Enums are serialized differently. Java serialization writes only the constant's name, and on reading it calls Enum.valueOf() to find the existing constant. Any readObject() or readResolve() methods on an enum are ignored.
One consequence is that the fields of an enum are not serialized at all. In our Registry, the handlers map lives only in memory; deserializing gives back the same INSTANCE with whatever handlers this JVM has registered.
5Reflection Cannot Create a Second Instance
For a normal class, setAccessible(true) on a private constructor lets any code call it. For enums, the JDK refuses at the point of creation: Constructor.newInstance() checks whether the class is an enum and throws.
The constructor's parameters are the name and ordinal that the compiler adds to every enum constructor. Together with the private constructor and serialization by name, this makes INSTANCE a true singleton: no code path can produce a second one.
6Limits, and When to Choose Something Else
The enum singleton has real limits. An enum implicitly extends java.lang.Enum, so it cannot extend another class, although it can implement interfaces. It is initialized eagerly when the class initializes, so an expensive constructor runs at that point, and a failure there makes the class unusable for the rest of the JVM's life, with NoClassDefFoundError on later access. And because INSTANCE is global, code that uses it hides a dependency and tests share its state between them.
So Effective Java's advice is about how to implement a singleton when we need one, not about whether to. In a Spring application, most services are already singletons in the container, injected where needed and easy to replace in tests, which is usually better. The enum singleton fits stateless strategies, small registries of constants, and code outside any container, such as a library utility.
7Follow-up questions
How does the enum singleton compare with the holder idiom?
The holder idiom, a private static nested class whose static field holds the instance, is lazy until the instance is first requested and is also thread-safe through class initialization. But it still needs readResolve() for serialization and gives no protection against reflection. The enum covers both, at the cost of not being able to extend a class.
Is the state of an enum singleton thread-safe automatically?
No. Only the creation is. Fields that change after creation are shared by all threads, so they need concurrent collections, atomics or locks like any shared mutable state.
Can we make the enum singleton lazy?
Its instance is created on class initialization, which already happens only when the enum is first used. If the expensive part must wait until a particular method is called, we can hold that part in a lazily initialized field inside the enum, but at that point dependency injection is usually the cleaner design.
8Conclusion
In this answer, we wrote an enum singleton with one constant, INSTANCE, holding a ConcurrentHashMap of handlers. The JVM creates it once during class initialization, which is thread-safe without any locking code. Serialization writes only the name and resolves it to the existing constant, and reflection cannot create enum instances. Its limits are that it cannot extend a class, it is created eagerly at class initialization, and, like any global, it hides dependencies, so in container-managed applications we usually prefer injected singletons.
Open with
I would write enum Registry with a single constant INSTANCE, and give it a private final ConcurrentHashMap of String to Handler with register and find methods. Callers use Registry.INSTANCE.register.
Then, if they want more
Effective Java calls this the best approach in many cases because it closes the three gaps classic singletons have. First, thread safety: INSTANCE is created in the enum's static initializer, and the JVM runs class initialization exactly once under its own lock and publishes the result to all threads, so there is no double-checked locking or volatile to get wrong.
Second, serialization: enums are serialized by name and resolved with valueOf on read, so you always get the existing constant without writing readResolve. Third, reflection: Constructor.newInstance refuses to create enum objects, so even setAccessible cannot make a second one. The state inside still has to be thread-safe, which is why I use a concurrent map.
The limits are that an enum cannot extend another class, though it can implement interfaces, and it is created eagerly when the class initializes, so a slow or failing constructor hurts. And it is still a global, so in a Spring application I would normally prefer a container-managed singleton bean that I can inject and replace in tests.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Implement a singleton using an enum and explain why Effective Java calls it the best...": https://scrivio-interview-library.netlify.app/java/enums/775332e444/