Interview Q&A · Java · 5 questions in this topic
Enums: Rules and Restrictions — Java Interview Q&A
How enums model a fixed set of constants as full classes, and the rules the compiler enforces on them. Part of Interfaces, Enums & Features · 294 questions in the Java bank.
What is an enum?
Answer
An enum, available since Java 5, is a special class that represents a fixed set of named constants such as days, directions or states. Despite the compact syntax, each enum is a full class that may declare fields, constructors and methods. Every enum implicitly extends java.lang.Enum, so its constants are public, static and final instances created once by the JVM.
Key points
- 1Enum constants are type-safe singletons: the compiler prevents invalid values at compile time.
- 2Enums work directly in switch statements and carry reliable equals, hashCode and toString behaviour.
- 3Constructors, fields and methods let each constant carry associated data and behaviour.
Example
public enum Day { MON, TUE, WED, THU, FRI, SAT, SUN; } Day today = Day.MON;
What restrictions apply to enums?
Answer
An enum cannot extend any class or enum because it already extends java.lang.Enum implicitly, though it is free to implement interfaces. It cannot be instantiated with new because its constants are the only permitted instances and are created by the JVM. Constructors must therefore stay private or package-private, and when an enum declares fields or methods the constant list must come first, terminated by a semicolon.
Key points
- 1No extends clause is allowed, but implements is allowed for shared behaviour.
- 2No new on an enum type: reflection-style instantiation is rejected as well.
- 3Constants-first ordering is enforced as soon as fields, constructors or methods appear.
💡 If asked why enum constructors are private, answer that the JVM alone creates the fixed constant set.
Can an enum have fields, constructors and methods?
Answer
Yes — an enum is a real class with a fixed set of instances. You can declare fields, a constructor (implicitly private, and it cannot be public or protected), methods, and even a different body per constant. The constants are created once, when the enum class is initialised, and passed to the constructor in declaration order. That makes enums ideal for attaching stable codes, labels or behaviour to a closed set of values.
Key points
- 1Enum constructors are always private; the compiler enforces it.
- 2Constants are instantiated once at class initialisation.
- 3Constant-specific class bodies allow per-value overrides.
- 4values() and valueOf() are generated automatically.
Example
enum Plan { FREE(0), PRO(10); private final int limit; Plan(int limit) { this.limit = limit; } int limit() { return limit; } }
How are enum constants serialized?
Answer
By name, not by the usual object graph. The JVM guarantees that deserializing an enum produces the same instance that already exists, so reference equality keeps working and no duplicate constant is created. Fields you add to an enum are not carried across serialization, and readResolve is effectively built in — which is why enums are the simplest safe singleton.
Key points
- 1Enum serialization writes the constant name.
- 2Deserialization returns the existing constant instance.
- 3Extra fields on the enum are not restored.
- 4Enums are the recommended way to implement a singleton.
How do enums work with switch, and what happens on null?
Answer
A switch over an enum uses unqualified constant names as case labels, and the compiler checks that every label belongs to that enum type — a benefit you do not get with int constants. Switching on a null reference throws NullPointerException before any case is considered, so guard for null first. Modern switch expressions add exhaustiveness checking and arrow syntax, which removes the fall-through bugs of the classic form.
Key points
- 1Case labels are written without the enum type prefix.
- 2The compiler verifies labels against the switched enum.
- 3A null selector throws NullPointerException.
- 4Switch expressions over enums must be exhaustive.
Example
switch (plan) { case FREE -> System.out.println("free tier"); case PRO -> System.out.println("paid tier"); }
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission