Here is an example of how to use an enum in a switch statement in Java:
public enum Color {
RED,
GREEN,
BLUE;
}
public class ColorDemo {
public static void main(String[] args) {
Color color = Color.RED;
switch (color) {
case RED:
System.out.println("Red");
break;
case GREEN:
System.out.println("Green");
break;
case BLUE:
System.out.println("Blue");
break;
}
}
}
Red
As you can see, the switch statement is used to execute different codes based on the value of the color
variable. In this case, the code will print out the name of the color that is stored in the color
variable.
Here is another example of how to use an enum in a switch statement in Java:
public enum DayOfWeek {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
public class DayOfWeekDemo {
public static void main(String[] args) {
DayOfWeek dayOfWeek = DayOfWeek.MONDAY;
switch (dayOfWeek) {
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;
}
}
}
This code will print out the following output:
It's Monday!
As you can see, the switch statement can be used to execute different codes based on the value of an enum variable. This can be a very convenient way to write code that checks for a specific value.