Enums
18 questions in 6 subtopics.
Enum basics and values()
What does the compiler generate forenum Status { ACTIVE, SUSPENDED }? Explain values(), valueOf(), ordinal() and name().A service stores enum ordinal() values in a database column. After someone adds a constant in the middle of the enum, existing data maps to wrong statuses. Explain and fix.A hot path calls Status.values() inside a loop and a profiler shows allocation from it. Why, and what do you do?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.Enums with fields and behavior
Write an enum Operation with PLUS and TIMES, each holding a symbol and implementingapply(int a, int b), in two ways: constant-specific bodies and a lambda field.An enum maps external codes to constants with a static lookup map. Show the pattern and explain why a constructor cannot put itself into a static map of the same enum.A shared library exposes a public enum Country used by thirty services. A new value must be added every few months, and some services break each time. How do you redesign this?Teams put business rules in enums (pricing tiers with rates, state machines with transitions). When is that good design and when does it become a problem across many services?EnumSet and EnumMap
How areEnumSet and EnumMap implemented, and why are they faster than HashSet and HashMap with enum keys?Replace this flag representation with EnumSet and show how to persist it compactly: int permissions = READ | WRITE; // constants 1, 2, 4, 8Enum singleton
Implement a singleton using an enum and explain why Effective Java calls it the best approach for many cases.A codebase uses enum singletons and static singletons for services such as caches and clients. Testing and configuration are painful. What would you propose?Effective Java recommended the enum singleton, while modern Spring applications manage singletons through the container. How has the role of singletons changed in Java over twenty years, and what would you allow in new code?Enum in switch
Write a switch expression over anenum Size { S, M, L } returning a price, without a default branch. What happens when a constant XL is added later?Should switches over enums include a default branch? Discuss the trade-off between compile-time exhaustiveness and run-time safety for enums that come from other services.Enum serialization
How does Java serialization handle enums, and how do JSON libraries such as Jackson serialize them by default? What breaks when a constant is renamed?A service fails to deserialize events after the producer team added a new enum constant to a shared event schema. Several consumers crashed. How do you fix the incident and prevent recurrence?Across years of operation, enum values have been persisted as ordinals, names and custom codes in databases, caches and messages, causing several data incidents. What long-term rules would you set?No question matches these choices.