Enums
Type-safe constants with fields, constructors and behaviour, plus EnumSet, EnumMap and JPA persistence.
#Introduction
An Enum (Enumeration) is a special Java type used to represent a fixed set of constants.
Instead of using hardcoded values like:
String status = "ACTIVE";we use:
Status status = Status.ACTIVE;Enums improve:
- Type Safety
- Readability
- Maintainability
- Compile-time checking
Enums were introduced in Java 5 and are heavily used in enterprise applications.
#What is an Enum?
An Enum defines a predefined list of constants.
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}Usage:
Day today = Day.MONDAY;
System.out.println(today);MONDAY#Why Enums?
Instead of:
String paymentType = "CARD";someone may accidentally write:
paymentType = "CRAD";The compiler won't detect the mistake. Using an Enum:
PaymentType type = PaymentType.CARD;Invalid values are rejected during compilation.
#Internal Working
Every Enum automatically extends java.lang.Enum. Internally,
enum Status {
ACTIVE,
INACTIVE
}behaves roughly like:
final class Status extends Enum<Status> {
public static final Status ACTIVE;
public static final Status INACTIVE;
}#Creating an Enum
public enum Status {
ACTIVE,
INACTIVE,
BLOCKED
}Usage:
Status status = Status.ACTIVE;#Enum Constants are Singleton Objects
Each enum constant exists only once.
- Status
- ACTIVE
- INACTIVE
- BLOCKED
Therefore, Status.ACTIVE == Status.ACTIVE always returns true.
#Enum Methods
Every Enum provides useful methods.
values()
Returns all constants.
for (Status s : Status.values()) {
System.out.println(s);
}ACTIVE
INACTIVE
BLOCKEDvalueOf()
Converts a String to an Enum.
Status status = Status.valueOf("ACTIVE");ACTIVEInvalid value:
Status.valueOf("TEST");IllegalArgumentExceptionordinal()
Returns the position.
System.out.println(Status.ACTIVE.ordinal());
System.out.println(Status.BLOCKED.ordinal());0
2name()
Returns the constant name.
Status.ACTIVE.name();ACTIVEtoString()
Default implementation:
System.out.println(Status.ACTIVE);ACTIVECan be overridden.
#Enum with Fields
Enums can contain instance variables.
public enum PaymentStatus {
SUCCESS(200),
FAILED(500),
PENDING(100);
private final int code;
PaymentStatus(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}Usage:
System.out.println(PaymentStatus.SUCCESS.getCode());200#Enum with Methods
public enum Direction {
NORTH,
SOUTH,
EAST,
WEST;
public boolean isNorth() {
return this == NORTH;
}
}Usage:
Direction.NORTH.isNorth();true#Enum with Constructors
Enums can have constructors.
enum Size {
SMALL(30),
MEDIUM(40),
LARGE(50);
private final int value;
Size(int value) {
this.value = value;
}
}Rules:
- The constructor is always private.
- You cannot create enum objects using
new.
Wrong:
new Size(); // Compilation Error#Enum with Abstract Methods
Each constant can have different behaviour.
public enum Operation {
ADD {
@Override
public int apply(int a, int b) {
return a + b;
}
},
SUBTRACT {
@Override
public int apply(int a, int b) {
return a - b;
}
};
public abstract int apply(int a, int b);
}Usage:
Operation.ADD.apply(10, 20);30#Enum in Switch
Enums work naturally with switch.
switch (status) {
case ACTIVE:
System.out.println("User Active");
break;
case BLOCKED:
System.out.println("User Blocked");
break;
default:
System.out.println("Unknown");
}#EnumSet
A special high-performance Set for enums.
EnumSet<Day> weekends = EnumSet.of(
Day.SATURDAY,
Day.SUNDAY
);Faster than HashSet for enum types.
#EnumMap
A special Map optimized for enum keys.
EnumMap<Status, String> map =
new EnumMap<>(Status.class);Advantages:
- Faster than
HashMap - Less memory
- Type-safe
#Enums in Spring Boot
Common examples: OrderStatus, PaymentStatus, UserRole, Gender, TicketStatus, NotificationType.
Entity:
@Enumerated(EnumType.STRING)
private Status status;#Why Use EnumType.STRING?
Suppose the enum is ACTIVE, INACTIVE, BLOCKED, stored in the database as ordinals 0, 1, 2. Later a new constant is inserted:
| Constant | Old ordinal | New ordinal |
|---|---|---|
ACTIVE | 0 | 0 |
SUSPENDED | — | 1 |
INACTIVE | 1 | 2 |
BLOCKED | 2 | 3 |
Ordinals change, so old data becomes incorrect. With STRING, values like ACTIVE and BLOCKED remain stable.
#Comparing Enums
Recommended:
if (status == Status.ACTIVE) {
}Why? Each enum constant is a singleton. Using == is safe and slightly faster than equals().
#Real Project Examples
| Enum | Constants |
|---|---|
| User Role | ADMIN, CUSTOMER, MANAGER, SUPER_ADMIN |
| Order Status | PLACED, CONFIRMED, SHIPPED, DELIVERED, CANCELLED |
| Payment | SUCCESS, FAILED, PENDING |
| Notification Type | EMAIL, SMS, PUSH, WHATSAPP |
#Interview Questions
Q1What is an Enum?
A special Java type representing a fixed set of constants.
Q2Can an Enum extend another class?
No. It already extends java.lang.Enum.
Q3Can an Enum implement interfaces?
Yes. Enums can implement one or more interfaces.
Q4Can an Enum have constructors?
Yes. Constructors are implicitly private.
Q5Can we create an Enum object using new?
No. Enum instances are created only by the JVM.
Q6Why use == for Enum comparison?
Each enum constant is a singleton, so reference comparison is safe.
Q7Should we store ordinal() values in the database?
No. Use EnumType.STRING.
Q8Can Enums contain methods?
Yes. They can contain fields, methods, constructors, and abstract methods.
Q9What are EnumSet and EnumMap?
Specialized, high-performance collections designed specifically for enum types.
Q10Why are Enums better than String constants?
- Compile-time safety
- No spelling mistakes
- Better readability
- Better switch support
- Easier maintenance
#Common Mistakes
- Comparing enum names using Strings.
- Persisting
ordinal()values. - Creating constants using integer values instead of enums.
- Using
equals()when==is sufficient.
#Best Practices
- Use enums whenever values are fixed.
- Use
==for enum comparisons. - Store enums as strings in databases.
- Keep business logic inside enums when appropriate.
- Use
EnumSetandEnumMapfor better performance.
#Quick Revision
#Interview Cheat Sheet
| Requirement | Recommendation |
|---|---|
| Fixed values | Enum |
| Database storage | EnumType.STRING |
| Comparison | == |
| Collection | EnumSet |
| Map key | EnumMap |
| Convert String | valueOf() |
#Chapter Summary
After completing this chapter, you should be able to:
- Explain what an Enum is and why it is used.
- Understand the internal working of enums.
- Use enum fields, constructors, and methods.
- Work with
EnumSetandEnumMap. - Persist enums correctly using
EnumType.STRING. - Confidently answer advanced enum interview questions commonly asked in Java and Spring Boot interviews.