Enums in Java — More Than Constants
Fast recall
Quick revision
Core answer
private. Compare with == (null-safe, never throws NPE). Constant-specific abstract methods = Strategy pattern without class hierarchy — compiler forces implementation per constant. EnumSet/EnumMap for collections (faster than HashSet/HashMap). Thread-safe and serialization-safe by design.Whether the candidate knows that Java enums are full classes — they can have fields, constructors, methods, and even implement interfaces. The interviewer wants to see if they understand enum's role in replacing integer constants and strategy patterns
Treating enums as simple constants like C-style enums. Comparing enum values with equals() instead of == (both work, but == is null-safe and faster). Not knowing that enum constructors are implicitly private and each constant is a singleton instance
Show how enums can implement the Strategy pattern by having each constant override an abstract method. Mention that enums are inherently serialization-safe (no duplicate instance problem), thread-safe (initialized in class loading), and work in switch statements including the newer switch expressions
Interview-ready guide
Interview answer
Java enums aren't named integers — they're full classes where each constant is a singleton instance with its own fields, methods, and interface implementations.
Why they're more powerful than constants
- Each constant is a singleton created once at class loading; constructors are implicitly private, so the instance set is fixed at compile time
- Compare with
==, notequals()— both work, but==is null-safe and faster for singletons - Use
EnumSet/EnumMapfor collections — bit-vector and array-backed, faster thanHashSet/HashMap
The strategy pattern without a class hierarchy
- Define an
abstractmethod in the enum and override it per constant:PREMIUM { public double applyDiscount(double p) { return p * 0.85; } } - Adding a new constant forces you to implement the method at compile time — a switch statement would silently ignore new values
Built-in safety properties
- Thread-safe: JVM initializes enums during class loading, once, safely
- Serialization-safe: deserialization returns the existing singleton, not a new duplicate — no broken
==identity
I recommend enums wherever you have a closed fixed set of options with associated behavior — not just as a replacement for integer constants.
Enums in Java — More Than Constants
Direct Answer
Java enums are full classes — each constant is a singleton instance created at class loading. Constructors are implicitly private. Compare with == (null-safe, never throws NPE). Constant-specific abstract methods = Strategy pattern without class hierarchy — compiler forces implementation per constant. EnumSet/EnumMap for collections (faster than HashSet/HashMap). Thread-safe and serialization-safe by design.
Common Mistakes & Pitfalls
Treating enums as simple constants like C-style enums. Comparing enum values with equals() instead of == (both work, but == is null-safe and faster). Not knowing that enum constructors are implicitly private and each constant is a singleton instance
Check your understanding
Answer these without looking back at the article.
- Why is it safe to compare enum values with == instead of equals()?
- How do you implement the Strategy pattern using constant-specific abstract methods in an enum?
- Why are enums inherently thread-safe and serialization-safe?
- What is the performance advantage of EnumSet and EnumMap over HashSet and HashMap?
- Can a Java enum implement an interface? Can it extend a class?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.