Scrivio Interview library
ConceptPrincipal6 min read · updated 12 November 2026

Java added enums in Java 5, replacing int constants. Explain the design decisions behind Java enums (classes, not integers) and how later features (switch expressions, sealed types, records) changed how you would model closed sets today.

Java 5 made enums full classes rather than named integers, so they are type-safe, can carry fields and methods and implement interfaces, and each constant is a guaranteed singleton; that design made EnumSet, EnumMap and safe serialization by name possible. Switch expressions later added compile-time exhaustiveness checks over enums, and sealed interfaces with records now model closed sets whose variants carry different data. Our rule of thumb: an enum when every member has the same shape, sealed records when the variants carry different data.

Why this question is asked

Before Java 5, a closed set of values was usually a handful of public static final int constants, and many production bugs came from passing the wrong int to the wrong method. Java's answer was unusually rich for its time, and features added fifteen years later, switch expressions, sealed types and records, build directly on it. Senior interviewers ask this to see whether we understand the design reasons, not only the syntax, and can choose between the old and new tools. In this answer, we will look at what int constants got wrong, what the class-based design bought, how switch expressions and sealed types changed the picture, and how to choose today.

1What Integer Constants Got Wrong

The pre-Java 5 pattern looked harmless. Each value was a static int, and methods took an int parameter. The compiler could not tell a size from a colour, because both were just numbers.

Java
// Before Java 5
public static final int SIZE_SMALL = 0, SIZE_LARGE = 1;
public static final int COLOR_RED = 0, COLOR_BLUE = 1;
void order(int size, int color) { ... }
order(COLOR_BLUE, SIZE_SMALL); // compiles: arguments swapped, nobody notices
order(42, 7); // compiles: values that do not exist
System.out.println(SIZE_LARGE); // 1: logs and debuggers show numbers, not names

There were more problems. The values were compiled into every class that used them, so changing a number required recompiling all clients. There was no way to iterate over all sizes, and no natural place to put behaviour that belongs to each value. C-style enums in other languages were essentially the same thing with nicer syntax.

2What the Class-Based Design Bought

Java's enum is a class whose instances are fixed. Each constant is an object, so a method that takes a Size cannot receive a Color or the number 42. Constants print their names, can hold fields and methods, and can implement interfaces. The JVM guarantees that each constant is a singleton: constructors are private, reflection refuses to create instances, and deserialization resolves the name to the existing constant.

That guarantee unlocked further designs. Because the set is fixed and each constant has a stable ordinal for the life of the program, EnumSet can represent a set of constants as bits in a long, and EnumMap can store values in an array indexed by ordinal. Because constants are serialized by name, not by field contents, a deserialized constant is the same object as the one in memory, and == stays correct.

Java
enum Size {
SMALL(250), LARGE(500);
final int millilitres;
Size(int ml) { this.millilitres = ml; }
}
void order(Size size, Color color) { }
order(Color.BLUE, Size.SMALL); // compile error: types do not match
EnumSet<Size> available = EnumSet.of(Size.SMALL); // a single long underneath
System.out.println(Size.LARGE + " " + Size.LARGE.millilitres); // LARGE 500

3Switch Expressions Add Exhaustiveness

For years, switching over an enum had a weak spot: if someone added a constant, every switch that forgot it silently fell through or did nothing. Switch expressions, standard since Java 14, fixed this. A switch expression must produce a value for every input, so when it has no default branch the compiler checks that every constant is covered.

Java
int price = switch (size) {
case SMALL -> 3;
case LARGE -> 5;
}; // no default needed: all constants covered
// After adding MEDIUM to Size and recompiling:
// error: the switch expression does not cover all possible input values

So adding a constant now produces a compile error at every place that must handle it. That turns a whole class of forgotten-case bugs into build failures, which is far cheaper than finding them in production.

4Sealed Types and Records Model Variants With Different Data

Enums have one limit: every constant has the same fields, set once when the enum is defined. They cannot model a closed set whose members carry different, per-instance data. A payment is a card payment with a card token, a bank transfer with an IBAN, or a voucher with a code and an expiry. Before Java 17, the usual workaround was an enum plus nullable fields, which was error-prone.

Sealed interfaces, standard since Java 17, close a hierarchy to a listed set of implementations. Combined with records, compact immutable data classes from Java 16, and pattern matching for switch, standard since Java 21, they give us closed sets of variants with different data, and the same exhaustiveness checking.

Java
sealed interface Payment permits Card, BankTransfer, Voucher {}
record Card(String token, int amountCents) implements Payment {}
record BankTransfer(String iban, int amountCents) implements Payment {}
record Voucher(String code, LocalDate expires) implements Payment {}
String describe(Payment p) {
return switch (p) {
case Card c -> "card " + c.token();
case BankTransfer b -> "transfer from " + b.iban();
case Voucher v -> "voucher valid until " + v.expires();
}; // exhaustive: the compiler knows all permitted types
}

Notice the similarity with enums: a closed set, a switch without default, and a compile error if a new variant is added and not handled. The difference is that each variant is a type with its own data, and there can be any number of instances of each.

5Choosing Between Them Today

The two tools now share the closed-set guarantees, so the choice depends on the shape of the data. If every member is a single fixed value with the same fields, an enum is simpler and gives singletons, EnumSet, EnumMap and cheap persistence by name. If members carry different data, or many instances per kind, sealed interfaces with records fit.

Enum or sealed records?
QuestionEnumSealed interface + records
Members carry different fields?No, all share one shapeYes, each record has its own
Instances per memberExactly one, a singletonAny number
Exhaustive switchYes, switch expressionsYes, pattern switch
Sets and mapsEnumSet, EnumMapordinary collections
Typical usestatuses, sizes, rolespayments, events, commands

The two also combine well. A sealed hierarchy of events can carry an enum field for a simple category, and an enum can implement a sealed interface when one variant is a fixed constant. The real mistake to avoid is the old workaround: an enum with nullable fields that only some constants use, which is exactly what sealed records replace.

6Follow-up questions

Why did Java not just copy C enums?

Because C enums are integers, so they offered none of the type safety that motivated the feature. Making them classes cost a little memory per constant but gave type checking, names in logs, behaviour per constant and the singleton guarantee that the collections and serialization rely on.

Do classic switch statements over enums check exhaustiveness?

Not by default. An old-style switch statement over an enum compiles even if it misses constants. Only switch expressions and switches that use patterns or case null must be exhaustive, so we prefer switch expressions for logic that must handle every constant.

Can an enum implement a sealed interface?

Yes. An enum is a class, so it can be listed in a sealed interface's permits clause. That is useful when a closed set mixes fixed constants with records that carry data, and a pattern switch covers both.

7Conclusion

In this answer, we saw that Java made enums full classes to fix the problems of int constants: no type safety, numbers in logs, no behaviour and no way to iterate. The singleton guarantee made EnumSet, EnumMap and serialization by name possible. Switch expressions later added exhaustiveness checks, and sealed interfaces with records extended closed sets to variants with different data. Today we use enums for fixed constants of one shape and sealed records for variants that carry their own data.

What to say in the interviewAbout 80 seconds aloud

Open with

Java 5 deliberately made enums full classes instead of named integers. With int constants you could pass a colour where a size was expected, or any random number, logs showed numbers, values were compiled into clients, and there was no place for behaviour.

Then, if they want more

A Java enum is a type, so the compiler rejects the wrong argument, constants print their names, can have fields, methods and interfaces, and each one is a JVM-guaranteed singleton: private constructors, no reflective creation, and serialization by name that resolves to the existing constant.

That guarantee is what made EnumSet, a bit vector, and EnumMap, an array indexed by ordinal, possible. Later features changed how I model closed sets. Switch expressions are checked for exhaustiveness, so adding a constant breaks the build at every switch that must handle it, as long as there is no default.

Sealed interfaces with records and pattern switches give the same exhaustive checking for variants that carry different data, like payment types with a card token or an IBAN, which enums cannot express without nullable fields.

So my rule is an enum when every member has the same shape and is a single fixed value, and a sealed interface with records when variants carry their own data.

About this answer

Last updated
12 November 2026
Versions it names
Java 5, Java 17, Java 14, Java 16
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Java added enums in Java 5, replacing int constants. Explain the design decisions behind...": https://scrivio-interview-library.netlify.app/java/enums/0f16c2de24/