Enum in Switch case — Java
/**
*
* Java program to demonstrate how to use Enum in Switch case statement.
* Enum can be used in switch block similar to primitive int or enum int pattern.
* Enum can also be used in CASE statement to take appropriate action based upon
* Enum instance.
*
* @author Javin
*/
public class EnumInSwitch <
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FR >;
>
public static void main ( String args []) <
Day [] daysOfWeek = Day. values () ;
for ( Day today : daysOfWeek ) <
//Using Enum in Switch case statement
switch ( today ) <
case MONDAY:
System. err . println ( «Today is Monday learn example of How to use Java Enum in Switch» ) ;
break ;
case TUESDAY:
System. err . println ( «Tuesday, apply Enum in Switch just link primitive int» ) ;
break ;
case WEDNESDAY:
System. err . println ( «Wednesday, I confirm Java Enum can be used in Switch case» ) ;
break ;
case THURSDAY:
System. err . println ( «Thursday, Java Enum values() method return all enum in an array» ) ;
break ;
case FRIDAY:
System. err . println ( «Friday, Enum can also be used in case statement» ) ;
break ;
case SATURDAY:
System. err . println ( «Saturday, Enum in Java are compile time constant» ) ;
break ;
case SUNDAY:
System. err . println ( «Sunday, Using Enum in Switch is very easy» ) ;
break ;
Output:
Today is Monday learn example of using Java Enum in Switch
Tuesday, apply Enum in Switch just link primitive int
Wednesday, I confirm Java Enum can be used in Switch case
Thursday, Java Enum values () method return all enum in an array
Fr >case statement
Saturday, Enum in Java are compile time constant
Sunday, Using Enum in Switch is very easy
Java enum FAQ: Can you share a Java enum switch example, i.e., how to use an enum with a Java switch statement?
In my earlier Java enum examples tutorial, I demonstrated how to declare a simple Java enum , and then how to use an enum with a variety of Java constructs, including a Java switch statement, a for loop, and an if/then statement.
In this enum tutorial, I want to just focus on using an enum in a switch statement. Hopefully this enum/switch example adds a little more complexity to my earlier examples.
A Java enum switch statement example
In this enum/switch example, I first declare an enum type that looks like this:
Then in the main portion of the program, I refer to that enum , both in my main method, and in the “print” method that I call from the main method.
Let’s take a look at the Java source code for my enum example, and then I’ll describe it afterwards:
When you compile and run this code, the output looks like this:
The output is in this order because the enum begins on SUNDAY , and goes in order from there until SATURDAY .
Discussion
As with any Java program, the flow of control starts in the main method. Inside main I jump right in with this for loop:
This “enum for loop” iterates through all the values in the Day enum, using the values method that comes with Java’s enum type. That’s really the only “trick” in this code; the rest of it is a standard Java 5 for loop, and it calls the printTodaysThought method once for each constant in the Day enum.
The printTodaysThought method takes one Day value ( theDay ), and compares that variable against the constants that are shown in the switch statement. For the values MONDAY through THURSDAY I print one String; for FRIDAY I print a different string; and SATURDAY and SUNDAY print their own string. If a calling program manages to somehow call this method with a different Day value — something which should be really hard to do, unless I add a new value to the Day enum — flow of control will fall down to the default expression.
Summary
I hope this Java enum switch statement example has been helpful. Between my original Java enum tutorial and this tutorial, I hope it helps to see at least two examples of how to use a custom enum type with a switch statement (sometimes called a case statement).
Related Java enum content
As I finish up my Java enum series, here’s a collection of the Java enum tutorials I’ve written. Again, I hope you find them helpful:
Why can’t you switch on an enum in Java? It seems simple enough to do and would make for some convenient code. Also this question could apply to String ‘s. You can switch on a char , but not a String .
6 Answers 6
You definitely can switch on enums. An example posted from the Java tutorials.

You actually can switch on enum s, but you can’t switch on String s until Java 7. You might consider using polymorphic method dispatch with Java enum s rather than an explicit switch . Note that enum s are objects in Java, not just symbols for int s like they are in C/C++. You can have a method on an enum type, then instead of writing a switch , just call the method — one line of code: done!
First, you can switch on an enum in Java. I’m guessing you intended to say you can’t, but you can. char s have a set range of values, so it’s easy to compare. Strings can be anything.
A switch statement is usually implemented as a jump table (branch table) in the underlying compilation, which is only possible with a finite set of values. C# can switch on strings, but it causes a performance decrease because a jump table cannot be used.
Java 7 and later supports String switches with the same characteristics.

Switch on enums works fine.
Switches for strings are implemented in Java 7.