Enum Singleton — the safest Singleton in Java
Fast recall
Quick revision
Core answer
INSTANCE. Java initializes each enum class safely, normal reflective construction of enum constants is prohibited, and serialization resolves the named constant instead of creating a normal new object. The guarantee is still per loaded enum class, so separate class loaders or JVMs can have separate instances. It is a compact choice when static global access is genuinely intended, but it is eager on first enum-class initialization and less flexible than dependency injection.Interview-ready guide
Interview answer
An enum singleton is an enum with one constant, commonly INSTANCE. It is concise because Java handles safe class initialization, normal reflective construction of enum constants is prohibited, and standard serialization resolves the declared constant instead of creating a separate ordinary object. I still qualify the scope: another class loader or JVM process can load its own enum class and instance. The constant is created when the enum class is initialized, and the enum cannot extend another class. It is a good compact implementation when static global access is truly required, but in a Spring design I often prefer an injected managed bean because configuration, lifecycle, and tests are more flexible.
Enum Singleton — the safest Singleton in Java
Direct Answer
An enum singleton declares one enum constant, such as INSTANCE. Java initializes each enum class safely, normal reflective construction of enum constants is prohibited, and serialization resolves the named constant instead of creating a normal new object. The guarantee is still per loaded enum class, so separate class loaders or JVMs can have separate instances. It is a compact choice when static global access is genuinely intended, but it is eager on first enum-class initialization and less flexible than dependency injection.
Common Mistakes & Pitfalls
- Using it inside Spring. You lose DI, AOP proxies, and easy mocking — prefer singleton beans.
- Trying to extend a class. Enums can't; if you need inheritance use the holder pattern or a bean.
- Assuming mutable state is safe. Thread-safe initialization doesn't make mutable fields thread-safe — you'd still need synchronization.
- Forgetting it's eager. The instance is built at class load; if creation is expensive and may be unused, consider the Bill Pugh holder pattern for laziness.
Check your understanding
Answer these without looking back at the article.
- What are the differences between enum Singleton and double-checked locking Singleton?
- How does reflection break traditional Singleton implementations?
- How would you get lazy initialization with JVM guarantees (Bill Pugh holder)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.