Course contentsShow
← Java
Lesson 610 of 2,87014. EnumerationsFree lesson

Using Enums in switch Statements: Traditional Syntax

Learn how to switch on enum values using case labels without the enum type prefix.

Using Enums in switch Statements: Traditional Syntax

What you'll learn: How to write cleaner switch statements on enums by omitting the enum type name in case labels.

When you use an enum in a switch statement, Java allows—and actually requires—a special shorthand syntax for the case labels. Instead of writing the fully-qualified enum constant name, you write just the constant name itself, without the enum type prefix.

The Traditional Syntax Rule

When switching on an enum variable, your case labels must use the simple constant name:

enum TrafficLight {
    RED, YELLOW, GREEN
}

public class TrafficController {
    public void handleLight(TrafficLight light) {
        switch (light) {
            case RED:       // NOT case TrafficLight.RED
                System.out.println("Stop!");
                break;
            case YELLOW:
                System.out.println("Caution!");
                break;
            case GREEN:
                System.out.println("Go!");
                break;
        }
    }
}

Why This Syntax?

The compiler already knows the type of the switch expression (TrafficLight in this example), so repeating it in every case would be redundant. Java enforces this brevity—writing case TrafficLight.RED would actually cause a compilation error.

Think of It Like This

Imagine you're at a restaurant ordering from the dessert menu. You don't say "I'll have the dessert-menu-item chocolate cake"—you just say "chocolate cake" because everyone knows you're ordering from the dessert menu. The context is clear.

Similarly, inside a switch (light) block, the compiler knows you're matching against TrafficLight values, so you just name the constant directly.

Key Takeaway:
In traditional switch statements on enums, use only the constant name in case labels (like case RED), never the fully-qualified name (like case TrafficLight.RED)—the compiler knows the type from the switch expression.