MediumExperience level: 3-5 years1 min read

Why prefer enums over int/String constants, and how do you put behavior in an enum?

Reviewed by Gurusankar M.

Asked inAmazonDeloitteAccenture
#enum#constants#strategy#type safety
Report issue

Short Answer

Enums are type-safe, exhaustively switchable, serializable singletons. Unlike int/String constants they can't take an invalid value, and they can carry fields and abstract methods — turning a switch into polymorphism.

On this page

Concept Made Simple

Coffee Chat Question

“Why prefer enums over int/String constants, and how do you put behavior in an enum?”

Remember It Faster

Mind Map Answer

An int STATUS_ACTIVE = 1 accepts any int; an enum Status { ACTIVE, ... } accepts only valid members. Add a field/method and each constant becomes a tiny strategy object.

Learn by Doing

Hands-on Keyboard

java
enum Plan {
    FREE(0), PRO(999), ENTERPRISE(4999);
    private final int priceCents;
    Plan(int c) { this.priceCents = c; }
    int priceCents() { return priceCents; }
}
System.out.println(Plan.PRO.priceCents());
Output
999

Think Beyond the Expected

What If?

Why is an enum the recommended way to implement a singleton?

Enum singletons are serialization-safe and reflection-proof by construction — the JVM guarantees a single instance, unlike a private-constructor class that can be broken via reflection or deserialization.

Real World

Order states modeled as an enum with an abstract `next()` method replace sprawling switch statements and make illegal transitions impossible to represent.

Interviewer's Expectation

Keywords they're listening for:

  • type safety
  • fields + constructors
  • abstract methods per constant
  • enum singleton
  • EnumMap/EnumSet

Common Mistakes

  • Persisting enum ordinal() (breaks when order changes)
  • Using int/String constants instead of enums
  • Giant switch statements instead of per-constant behavior

Best Practices

  • Persist enums by name, never ordinal
  • Use EnumMap/EnumSet for enum-keyed collections
  • Put behavior in the enum to kill switch statements

Follow-up Questions

  1. What are EnumSet/EnumMap and why are they fast?
  2. How do you persist enums in JPA safely (name vs ordinal)?
  3. How does an enum implement a state machine?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: OOP (Core Java)
Interview question: "Why prefer enums over int/String constants, and how do you put behavior in an enum?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.