Enums (Fixed Sets of Values)
An enum is a type whose value can only be one of a fixed, named list — perfect for days, states and categories.
What you will learn
- Define and use an enum
- Switch on an enum value
- Know when an enum beats plain strings or numbers
When only certain values make sense
Some things have a small, fixed set of possible values: the days of the week, the suits in a deck of cards, the status of an order (NEW, PAID, SHIPPED). An enum (short for enumeration) is a type whose value must be one of a named list you define. Nothing else is allowed.
Why not just use Strings like "PAID"? Because a String could be misspelled ("piad") and Java would not notice. An enum value is checked by the compiler — a typo simply will not compile, so a whole class of bugs disappears.
public enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}Note: Output:
(No output yet — this defines a new type called Day. From now on a Day variable can ONLY hold one of these seven values. Each value, like Day.MONDAY, is written in capitals by convention.)
Using an enum
public class Main {
public static void main(String[] args) {
Day today = Day.WEDNESDAY;
System.out.println("Today is " + today);
if (today == Day.SATURDAY || today == Day.SUNDAY) {
System.out.println("It is the weekend!");
} else {
System.out.println("It is a working day.");
}
}
}Note: Output:
Today is WEDNESDAY
It is a working day.
today can only ever be one of the seven Days. We compared it with == to the weekend values. If we had typed Day.SATERDAY by mistake, the program would refuse to compile — the safety an enum gives you.
switch reads beautifully with enums
Because an enum has a known list of values, a switch over it is clear and complete. You do not even write the Day. prefix on each case.
public class Main {
static String mood(Day day) {
switch (day) {
case SATURDAY:
case SUNDAY:
return "Relax";
case FRIDAY:
return "Almost there";
default:
return "Keep going";
}
}
public static void main(String[] args) {
System.out.println(mood(Day.FRIDAY));
System.out.println(mood(Day.SUNDAY));
System.out.println(mood(Day.TUESDAY));
}
}Note: Output:
Almost there
Relax
Keep going
The switch matched each Day to a message. SATURDAY and SUNDAY fall through to the same "Relax" line (no break between them, on purpose). Anything not listed hits default. Switching on enums is one of their most pleasant uses.
Looping over every value
Every enum has a built-in values() method that gives all its options as an array — handy for menus or filling a dropdown:
for (Day d : Day.values()) {
System.out.println(d);
}Note: Output:
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
Day.values() returned all seven Days in the order you declared them, and the for-each loop printed each. This is the easy way to walk through every possible value of an enum.
Tip: Reach for an enum whenever a value should be one of a small, known set — order status, difficulty level, traffic-light colour. It is safer and clearer than scattering magic strings or numbers (like 0, 1, 2) through your code.
Q. Why use an enum instead of plain Strings like "PAID" and "SHIPPED"?
✍️ Practice
- Create an enum
Sizewith SMALL, MEDIUM, LARGE. Store one in a variable and print it. - Write a switch on a
TrafficLightenum (RED, AMBER, GREEN) that prints Stop, Get ready or Go.
🏠 Homework
- Make an enum
OrderStatus(NEW, PAID, SHIPPED, DELIVERED). Loop overvalues()to print each, then write a method that returns a friendly message for a given status using a switch.