An enum (enumeration) in Java is a special data type used to define a fixed set of constants.
Example uses:
- Days of the week
- Directions
- Order status
- User roles
Enums provide:
- Type safety
- Readability
- Organized grouping of constant values
enum Direction {
NORTH, SOUTH, EAST, WEST
}Using the enum:
Direction d = Direction.NORTH;
System.out.println(d);Output:
NORTH
-
Type-safe constants You cannot assign invalid values.
-
More powerful than
static finalvariables Enums can have:- Fields
- Methods
- Constructors
-
Prevents bugs Unlike integers/strings used as constants.
-
Readable and maintainable code
Internally, every enum extends:
java.lang.EnumHence, enum values are objects, not primitive values.
Example:
Direction.NORTHis actually an instance of enum Direction.
Enums can contain:
- Instance variables
- Constructors
- Methods
enum Status {
SUCCESS(200),
ERROR(500),
PENDING(102);
private int code;
Status(int code) { // constructor
this.code = code;
}
public int getCode() {
return code;
}
}Usage:
System.out.println(Status.SUCCESS.getCode()); // 200Enums come with built-in methods:
Returns all enum constants as an array.
for (Direction d : Direction.values()) {
System.out.println(d);
}Converts a String to an enum constant.
Direction d = Direction.valueOf("EAST");Returns position (index) of constant.
Direction.SOUTH.ordinal(); // 1Index starts from 0.
Enums work in switch statements.
Direction d = Direction.EAST;
switch(d) {
case EAST: System.out.println("Go Right"); break;
case WEST: System.out.println("Go Left"); break;
}You can define abstract methods and let each constant implement it.
enum Operation {
ADD {
int calculate(int a, int b) { return a + b; }
},
SUB {
int calculate(int a, int b) { return a - b; }
};
abstract int calculate(int a, int b);
}Usage:
System.out.println(Operation.ADD.calculate(5, 3)); // 8Enums cannot extend classes (already extends Enum),
but can implement interfaces.
interface Printable {
void print();
}
enum Color implements Printable {
RED, GREEN, BLUE;
public void print() {
System.out.println(this.name());
}
}enum Level {
LOW, MEDIUM, HIGH
}void check(Level l) {
switch(l) {
case LOW: System.out.println("Low level"); break;
case MEDIUM: System.out.println("Medium level"); break;
case HIGH: System.out.println("High level"); break;
}
}Java provides EnumSet for fast operations on enums.
EnumSet<Direction> set = EnumSet.of(Direction.NORTH, Direction.SOUTH);- Faster than HashSet
- Uses bit-vector internally
EnumMap<Direction, String> map = new EnumMap<>(Direction.class);
map.put(Direction.NORTH, "Up");
map.put(Direction.SOUTH, "Down");- Very efficient
- Keys must be enum type
| Feature | enum | static final |
|---|---|---|
| Type Safe | ✔ | ❌ |
| Can have methods | ✔ | ❌ |
| Can group related constants | ✔ | ✔ |
| Compile-time checking | ✔ | ❌ |
| Switch-case friendly | ✔ | ✔ |
| Extensible | Moderate | Low |
Enums are far superior for representing fixed sets.
enum HttpStatus {
OK(200), NOT_FOUND(404), SERVER_ERROR(500);
private int code;
HttpStatus(int c) { code = c; }
int getCode() { return code; }
}enum OrderStatus {
PLACED, SHIPPED, DELIVERED, CANCELLED
}enum Direction {
NORTH, SOUTH, EAST, WEST
}No. All enums implicitly extend java.lang.Enum.
Yes.
Yes, but it must be private.
Yes — each constant must implement them.
Type-safety, clarity, and richer functionality.
-
Enums are special classes with fixed constants.
-
They provide type safety and better structure.
-
They can contain fields, constructors, methods.
-
They can implement interfaces but cannot extend classes.
-
Used widely in modern Java applications (Spring, JPA, APIs).