Quick revision
The short answer
Putting rules in an enum is good design when the behaviour belongs to a small closed set that changes together with the code, because we get type safety and exhaustive switches. It becomes a problem when the values are things business users need to change, such as rates and limits, which then need a code release, and when the enum is shared across services, where every new constant needs a coordinated release and older consumers fail on unknown values. Across service boundaries we use string codes with documented unknown-value handling, and keep enums in API DTOs tolerant, for example with a fallback UNKNOWN constant.
What to remember
In this answer, we saw that enums with rules are good design when the set is small, closed and changes with the code, as in an internal state machine, because we get type safety and exhaustive switches. They become a problem when they hold values the business needs to change, which then require releases, and when they are shared across services, where each new constant needs a coordinated release and old consumers fail on unknown values. We move such values to configuration or data, exchange string codes across boundaries, keep DTO enums tolerant with an UNKNOWN fallback, and give teams a short decision guide.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Putting rules in an enum is good design when the behaviour belongs to a small closed set that changes together with the code, because we get type safety and exhaustive switches. It becomes a problem when the values are things business users need to change, such as rates and limits, which then need a code release, and when the enum is shared across services, where every new constant needs a coordinated release and older consumers fail on unknown values. Across service boundaries we use string codes with documented unknown-value handling, and keep enums in API DTOs tolerant, for example with a fallback UNKNOWN constant.
Background to this question
Enums with behaviour are one of Java's nicest features, and teams naturally start using them for pricing tiers, approval limits and order state machines. A year later the same enums are the reason a price change needs a deploy, or why five services fail the morning after another team's release. A senior engineer is expected to see both sides and set guidance the teams can follow. In this answer, we will look at where enums with rules shine, where they hurt inside one service and across many, what to use instead in each case, and how we would roll the guidance out.
1Where Enums With Rules Are Good Design
An enum is a fixed set of constants, each an object that can carry fields and methods. When the rules are tied to that set and change only when the code changes, putting them in the enum is excellent. A good example is the internal state machine of an order, where the allowed transitions are part of how the code works.
Here the enum gives us real benefits. A method that accepts an OrderState cannot receive a typo. The switch expression is exhaustive, so adding a state such as RETURNED fails the build until someone decides its transitions. And a new state needs new code anyway, for example a new screen or a new handler, so it naturally ships with a release. The rule and the code change together, which is the condition that makes an enum the right home.
2Problem 1: Values the Business Wants to Change
Now consider a pricing tier enum that holds discount rates.
The tiers themselves may be stable, but the rates are business decisions. Marketing wants GOLD at 12 percent for the holiday season, finance wants to test a new limit in one region. Each request becomes a ticket, a code change, a review, a build and a deploy, often several days for a one-number change, and the change is invisible to the people who own it.
The guidance is to separate the closed set from the values attached to it. The enum can still say which tiers exist, if that really is closed, but the rates move to configuration or to a table that the business can change through an admin tool, with an audit trail and an effective date. The test for a senior reviewer is simple: who decides when this value changes? If the answer is not the engineering team, the value is data.
3Problem 2: Shared Enums Couple Services
The bigger problem appears when an enum lives in a shared library or in a shared API model. Adding a constant is binary compatible in Java, but behaviour is not. A consumer that recompiles finds compile errors in its exhaustive switches. A consumer that does not upgrade meets the new value at run time, in a JSON payload it cannot parse or a switch that throws MatchException. So every new constant needs a coordinated release across all consumers, and the slowest team decides when the business can launch.
6 messages in order. Play them, or step through them one at a time.
The producer did nothing wrong by its own rules; the contract simply never said how unknown values are handled.
The root cause is that a Java enum in a contract promises a closed set to every consumer, while the producer's domain is not closed at all. Producers and consumers deploy independently, and stored data and queued messages outlive any one deployment.
4What We Use Instead Across Boundaries
For contracts between services, the guidance is to exchange strings, not shared Java enums, with documentation that lists the known values and states that new values may appear and how consumers must handle them. Each service can still map the string to its own internal enum, and that mapping is the one place where an unknown value is turned into something explicit.
When a team does want an enum in its API DTOs, the data classes used for JSON, the enum must be tolerant. With Jackson 2 this means a fallback constant marked with @JsonEnumDefaultValue and the deserialization feature READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE enabled. Jackson 3, which Spring Boot 4 uses, reorganised some of these features, so check your version for the exact names.
Notice that UNKNOWN is not a silent default. Code that meets it must decide what to do, such as charge no discount and log a metric, and the exhaustive switch makes sure every place considers it. That is very different from a default branch that quietly treats PLATINUM like BRONZE.
5Turning This Into Team Guidance
A senior engineer's job here is less about any one enum and more about giving teams a quick way to decide. We would write a short decision guide, add a lint rule that flags enums in published API modules, and review the existing shared enums one by one, starting with those that changed most often.
- Does the set change only when the code changes?YesContinueNoUse data: a table or configuration
- Do business users need to change the values?YesKeep the set in an enum, move values to configurationNoValues can stay in the enum
- Is the enum part of a contract with other services?YesExchange string codes, tolerant mapping insideNoAn internal enum is fine
- Make every switch exhaustiveno default that hides new constants
4 steps in order. Play them, or step through them one at a time.
For example, a team owning a shared Tier enum with rates would keep an internal Tier enum in its own service, move the rates to a pricing table with an admin screen, and change the event schema to carry tier as a string. Consumers map it to their own enums with an UNKNOWN fallback. The first holiday price change then takes minutes instead of a release, and the next new tier needs no coordinated deploy.
6Follow-up questions
Isn't moving rates to a table a loss of type safety?
The set of tiers can stay typed, and the table can be validated on load: every tier must have a rate, rates must be between 0 and 1, and the service refuses to start otherwise. We trade compile-time constants for load-time validation, which is the right trade for values that change on a business schedule.
What about state machines shared across services?
The states and transitions usually belong to one owning service. Other services should see events with string states and treat them as information, not re-implement the transitions. If several services need the rules, the owner exposes them through an API rather than a shared enum.
When is a default branch acceptable?
When the fallback behaviour is genuinely correct for any future constant, for example logging. For business decisions, an explicit UNKNOWN case is better, because the compiler then still checks that every known case is handled.
7Conclusion
In this answer, we saw that enums with rules are good design when the set is small, closed and changes with the code, as in an internal state machine, because we get type safety and exhaustive switches. They become a problem when they hold values the business needs to change, which then require releases, and when they are shared across services, where each new constant needs a coordinated release and old consumers fail on unknown values. We move such values to configuration or data, exchange string codes across boundaries, keep DTO enums tolerant with an UNKNOWN fallback, and give teams a short decision guide.
Open with
I think of it in terms of who changes the rule and who depends on it. An enum is great when the set is small and closed and changes together with the code, like the transitions of an internal order state machine: we get type safety, behaviour next to the constant, and exhaustive switch expressions that fail the build when someone adds a state.
Then, if they want more
It goes wrong in two situations. First, when the enum holds values the business wants to change, like discount rates or approval limits; then a one-number change needs a code release, so I keep the set typed if it is truly closed but move the values to configuration or a table with an admin path and audit.
Second, when the enum is shared across services. Adding a constant is binary compatible, but consumers that recompile get errors, old binaries throw MatchException or fail to parse the JSON, and every addition needs a coordinated release. So across service boundaries I exchange string codes with documented handling of unknown values, and each service maps them to its own enum.
If an enum appears in an API DTO, it gets an UNKNOWN constant with Jackson's read-unknown-enum-values-using-default-value enabled, handled explicitly in the code. I would capture this in a short decision guide and a lint rule for API modules.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3, Spring Boot 4
- 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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 "Teams put business rules in enums (pricing tiers with rates, state machines with...": https://scrivio-interview-library.netlify.app/java/enums/dbeab93185/