Clean • Professional
In Java, an Enum (Enumeration) is a special data type used to define a fixed set of constants.
Enums improve readability, maintainability, and type safety compared to using integers, strings, or booleans for representing options like status, roles, or types.
In simple words:
A special data type that defines a fixed set of named constants, making code type-safe, readable, and maintainable.
Example:
public enum UserStatus {
ACTIVE,
INACTIVE,
BLOCKED
}
Usage Example:
UserStatusstatus= UserStatus.ACTIVE;
if (status == UserStatus.BLOCKED) {
System.out.println("User is blocked!");
}
Enums are widely used in Spring Boot applications, especially with JPA entities, APIs, DTOs, and business logic.
ACTIVE, INACTIVE, BLOCKED).Enums can hold additional data and behavior:
publicenumOrderStatus {
PENDING(0),
SHIPPED(1),
DELIVERED(2),
CANCELLED(3);
privatefinalint code;
OrderStatus(int code) {
this.code = code;
}
publicintgetCode() {
return code;
}
publicstatic OrderStatusfromCode(int code) {
for (OrderStatus status : OrderStatus.values()) {
if (status.getCode() == code)return status;
}
thrownewIllegalArgumentException("Invalid code: " + code);
}
}
Usage:
OrderStatusstatus= OrderStatus.fromCode(1);// SHIPPED
System.out.println(status.getCode());// 1
Use enums to represent status, type, or role fields in entities. Map them to database columns using @Enumerated.
@Entity
publicclassUser {
@Id
@GeneratedValue
private Long id;
private String name;
@Enumerated(EnumType.STRING)// Stores enum name in DB
private UserStatus status;
// Getters & setters
}

| EnumType | Description | Pros | Cons |
|---|---|---|---|
| EnumType.ORDINAL | Stores the enum name in the database (e.g., "ACTIVE") | Safe if enum order changes, readable | Uses slightly more storage |
| EnumType.STRING | Stores the enum ordinal (0, 1, 2…) | Compact storage | Risky if enum order changes → can cause data inconsistency |
—> Recommended: Always use EnumType.STRING for clarity and maintainability.
Java enums can be classified based on complexity and usage:

Example:
publicenumDay {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Example:
publicenumStatus {
NEW(1),
IN_PROGRESS(2),
COMPLETED(3);
privatefinalint code;
Status(int code) {
this.code = code;
}
publicintgetCode() {
return code;
}
}
Can have custom methods to provide extra behavior.
Example:
publicenumOrderStatus {
PENDING, SHIPPED, DELIVERED;
publicbooleanisFinal() {
returnthis == DELIVERED;
}
}
Example:
publicenumPriority {
HIGH(3), MEDIUM(2), LOW(1);
privatefinalint level;
Priority(int level) {
this.level = level;
}
publicintgetLevel() {
return level;
}
}
Enums can implement interfaces for polymorphic behavior.
Example:
interfaceAlert {
voidshow();
}
publicenumAlertTypeimplementsAlert {
INFO {
publicvoidshow() { System.out.println("Info alert"); }
},
WARNING {
publicvoidshow() { System.out.println("Warning alert"); }
};
}
You can use enums directly in repository query methods to ensure type safety and avoid magic strings.
@Repository
publicinterfaceTaskRepositoryextendsJpaRepository<Task, Long> {
// Find all tasks with a specific status
List<Task>findByStatus(Status status);
}
Usage Example:
// Fetch all tasks with status IN_PROGRESS
List<Task> tasks = taskRepository.findByStatus(Status.IN_PROGRESS);
// Iterate and print
tasks.forEach(task -> System.out.println(task.getName() +" - " + task.getStatus()));
Key Points:
@Enumerated.Enums can store extra info like codes, descriptions, or priorities:
publicenumPriority {
HIGH(3),
MEDIUM(2),
LOW(1);
privatefinalint level;
Priority(int level) {
this.level = level;
}
publicintgetLevel() {
return level;
}
}
Usage in Entity:
@Enumerated(EnumType.STRING)
private Priority priority;
Enums can be used directly in Spring Boot REST APIs and DTOs:
Example Controller
@RestController
@RequestMapping("/users")
publicclassUserController {
@PostMapping
public StringcreateUser(@RequestParam UserStatus status) {
return"Created user with status: " + status;
}
}
Example Request
POST /users?status=ACTIVE
Behavior:
"ACTIVE" to UserStatus.ACTIVE.Benefits
EnumType.STRING in JPA to avoid ordinal-based issues.Enums are a powerful tool in Java and Spring Boot for defining fixed sets of constants in a type-safe and maintainable way.
They are ideal for status fields, roles, types, and integrate seamlessly with JPA entities, DTOs, repositories, and APIs. Using @Enumerated(EnumType.STRING) ensures safe and clear database mapping.
Enums can include fields, constructors, and methods for advanced logic. Following best practices—like avoiding magic strings and delegating logic to services—helps build robust, readable, and enterprise-ready applications.