Java > Core Java > Control Flow > Switch-Case Statements
Switch-Case with Enums
This example demonstrates the use of enums with switch-case statements. Enums provide a more type-safe and readable way to represent a fixed set of values.
Code
In this example, an enum `Day` is defined to represent the days of the week. The `switch` statement then evaluates the `today` variable, which is of type `Day`. Each `case` label corresponds to one of the enum constants. The code is more readable and type-safe because the compiler can verify that all possible enum values are handled (or a `default` case is present).
public class EnumSwitchExample {
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args) {
Day today = Day.WEDNESDAY;
switch (today) {
case SUNDAY:
System.out.println("It's Sunday!");
break;
case MONDAY:
System.out.println("It's Monday!");
break;
case TUESDAY:
System.out.println("It's Tuesday!");
break;
case WEDNESDAY:
System.out.println("It's Wednesday!");
break;
case THURSDAY:
System.out.println("It's Thursday!");
break;
case FRIDAY:
System.out.println("It's Friday!");
break;
case SATURDAY:
System.out.println("It's Saturday!");
break;
default:
System.out.println("Invalid day"); // This should never happen, but good practice to include
}
}
}
Concepts Behind the Snippet
Enums are a special data type that enables a variable to be a set of predefined constants. Using enums with switch-case
statements improves code readability and maintainability by providing a type-safe way to handle a limited set of values. The compiler can also provide better error checking when using enums in switch
statements.
Real-Life Use Case
Consider a game where you need to handle different game states (e.g., MENU, PLAYING, PAUSED, GAME_OVER). Using an enum for game states and a switch-case
statement to handle transitions between states is a clean and efficient approach.
Best Practices
switch-case
statement (or provide a default
case).
Interview Tip
Be prepared to discuss the benefits of using enums with switch-case
statements. Explain how it improves type safety and code readability.
When to Use Them
Use enums with switch-case
statements when you have a fixed set of named constants and you want to execute different code blocks based on which constant is selected.
Memory Footprint
Enums generally have a small memory footprint. Each enum constant is represented as a single instance of the enum class. The memory usage is typically negligible.
Alternatives
Alternatives include using a combination of if-else
statements and constant values, but this approach is less type-safe and maintainable.
Pros
Cons
FAQ
-
Is it necessary to have a `default` case when using enums in switch statements?
While not strictly required, it's good practice to include a `default` case, even if it just throws an exception, to handle unexpected enum values or future additions to the enum. -
Can I have a switch statement without any 'case' labels?
Yes, technically, you can have a switch statement without any 'case' labels (or even a default label). In this scenario, the switch block will simply be skipped, and execution will continue with the next statement after the switch block. This might be useful in rare scenarios where you want to conditionally skip a block of code based on a variable.