Interview Q&A · Java · 5 questions in this topic

Enums: Rules and Restrictions — Java Interview Q&A

All Java topics

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

  1. 1Enum constants are type-safe singletons: the compiler prevents invalid values at compile time.
  2. 2Enums work directly in switch statements and carry reliable equals, hashCode and toString behaviour.
  3. 3Constructors, fields and methods let each constant carry associated data and behaviour.

Example

Example.javajava
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

  1. 1No extends clause is allowed, but implements is allowed for shared behaviour.
  2. 2No new on an enum type: reflection-style instantiation is rejected as well.
  3. 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

  1. 1Enum constructors are always private; the compiler enforces it.
  2. 2Constants are instantiated once at class initialisation.
  3. 3Constant-specific class bodies allow per-value overrides.
  4. 4values() and valueOf() are generated automatically.

Example

Example.javajava
enum Plan {
    FREE(0), PRO(10);
    private final int limit;
    Plan(int limit) { this.limit = limit; }
    int limit() { return limit; }
}

Learn this topic

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits