library
intermediateEnums & Enum Methods
Use enums as type-safe constants with behavior — fields, methods, abstract methods, and implementing interfaces.
An enum is a class with a fixed set of instances. Each constant is a singleton object, not a number.
Enum = a deck of specific cards. You can't create new cards at runtime, and each card can have its own behavior (ace counts as 1 or 11).
Key Concepts
1
enum Status {
OPEN("Open"), CLOSED("Closed");2
private final String label;
Status(String label) { this.label = label; } // implicitly private
String label() { return label; }
}3
Which is the whole advantage over int constants.
static final int OPEN = 1;
setStatus(47); // compiles, and is nonsenseint
4
setStatus(Status.OPEN); // the only values that fit are the real ones5
Constants can each have their own behaviour, which turns a switch into dispatch:
6
enum Op {
PLUS { int apply(int a, int b) { return a + b; } },
TIMES { int apply(int a, int b) { return a * b; } };
abstract int apply(int a, int b);
}7
Op.TIMES.apply(3, 4); // 128
The methods you get for free.
values() every constant, in declaration order
valueOf("OPEN") the constant with that exact name
name() the declared name
ordinal() the 0-based position
compareTo by ordinal9
Do not store ordinal(). It changes the moment someone inserts a constant, and any persisted value then
means something different. Store name(), or an explicit code field.
ordinal()name()
10
valueOf throws on a miss, so guard it:
valueOf
11
try { return Status.valueOf(s); }
catch (IllegalArgumentException e) { return null; }12
An enum is the best singleton in Java. Constants are initialised once when the class loads, which gives
you thread safety, and serialisation cannot create a second copy — two problems the hand-written singleton
has to solve explicitly.
13
EnumSet and EnumMap are the right collections for enum keys. EnumSet is a bit vector and EnumMap
a plain array, so both are far faster than HashSet or HashMap here.
EnumSetEnumMapHashSetHashMap
14
Enums can implement interfaces but cannot be extended, and new is not allowed.
new
15
What the interviewer is probing.1. "Why is an enum the best singleton in Java?" Probing: whether you know what it gives you
for free. Stalls: "It is simple." Moves up: initialisation is thread-safe by class loading, and
serialisation and reflection cannot create a second instance — the three problems a hand-written
singleton must solve.
16
2. "Why should you never persist ordinal()?" Probing: the fragility. Stalls: "It is fine if
the order is stable." Moves up: inserting or reordering a constant silently changes every stored
value; persist name() or an explicit code field.
17
3. "How do you give each constant its own behaviour?" Probing: constant-specific method
bodies. Stalls: "With a switch." Moves up: declare an abstract method on the enum and implement
it per constant, which turns the switch into dispatch.
18
4. "Why are EnumSet and EnumMap faster than HashSet and HashMap?" Probing: whether you know
the implementation. Stalls: "They are specialised." Moves up: EnumSet is a bit vector and
EnumMap an array indexed by ordinal — no hashing, no collisions, no boxing.