Java Enums
An enum is a special class that holds a fixed group of named constants. Use an enum when a value can come from a short, known list, such as days of the week, traffic light colors, or order status.
A Traffic Light Example
A traffic light shows RED, YELLOW, or GREEN. No other color exists. Writing these three choices as an enum stops anyone from entering a color like "PURPLE" by mistake.
Diagram: A Fixed Menu of Values
enum Signal +---------------------------+ | RED YELLOW GREEN | +---------------------------+ A Signal variable holds exactly ONE of these three.
Creating an Enum
enum Signal {
RED, YELLOW, GREEN
}
public class Main {
public static void main(String[] args) {
Signal now = Signal.RED;
System.out.println(now); // RED
}
}
Write constant names in capital letters. Access each constant through the enum name.
Enum in a Switch
switch (now) {
case RED:
System.out.println("Stop");
break;
case YELLOW:
System.out.println("Get ready");
break;
case GREEN:
System.out.println("Go");
break;
}
Built-In Enum Methods
| Method | Result |
|---|---|
| values() | Array of all constants |
| valueOf("RED") | The constant with that name |
| name() | The constant name as text |
| ordinal() | Position number, starting at 0 |
for (Signal s : Signal.values()) {
System.out.println(s + " at position " + s.ordinal());
}
Output:
RED at position 0 YELLOW at position 1 GREEN at position 2
Enums with Fields and Constructors
Each constant can carry data. Add a field, a constructor, and a getter method.
enum Size {
SMALL(250), MEDIUM(400), LARGE(600);
private final int millilitres;
Size(int millilitres) {
this.millilitres = millilitres;
}
public int getMillilitres() {
return millilitres;
}
}
System.out.println(Size.LARGE.getMillilitres()); // 600
A juice shop can use this enum to look up cup sizes. The constructor runs once for each constant, and the constants stay constant.
Diagram: Constants Carrying Data
SMALL --> 250 ml MEDIUM --> 400 ml LARGE --> 600 ml
Enums with Methods
enum Level {
LOW, HIGH;
Level flip() {
return this == LOW ? HIGH : LOW;
}
}
Why Enums Beat Plain Numbers or Text
- The compiler rejects values outside the list
- Code reads like plain language
- Switch statements stay clean
- Each constant can hold its own data and methods
Common Mistakes
- Calling valueOf with a name that does not exist, which throws an IllegalArgumentException
- Using ordinal() for storage, since changing the order changes the numbers
- Trying to create an enum object with new, which Java blocks
Real Example: Order Status
An online shop tracks every order through a short list of stages. An enum lists the stages and stops typing errors such as "Shiped".
enum Status {
PLACED, PACKED, SHIPPED, DELIVERED
}
class Order {
Status status = Status.PLACED;
void moveNext() {
Status[] all = Status.values();
if (status.ordinal() < all.length - 1) {
status = all[status.ordinal() + 1];
}
}
}
PLACED --> PACKED --> SHIPPED --> DELIVERED
Each call to moveNext advances the order by one stage and stops at DELIVERED. Reports, screens, and emails all use the same four names, so the whole team speaks one language.
Comparing Enums
Compare enum constants with the double-equals sign. Each constant exists exactly once in memory, so the check is safe and fast.
if (order.status == Status.SHIPPED) {
System.out.println("On the way");
}
Quick Summary
- An enum defines a fixed list of constants
- values() and valueOf() help you read the list
- Enums can hold fields, constructors, and methods
