A Java enum switch/case statement example

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:

enum Day
{
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 
}

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:

/**
 * A Java enum switch statement (switch/case) example.
 * @author alvin alexander, https://alvinalexander.com
 */
public class JavaEnumSwitchCaseExample
{

  public static void main(String[] args)
  {
    // loop through the enum values, calling the
    // print method once for each value
    for (Day d: Day.values())
    {
      printTodaysThought(d);
    }
  }

  // a method that prints a String corresponding to the day value
  // that is passed in.
  public static void printTodaysThought(Day theDay)
  {
    switch (theDay)
    {
      case MONDAY:
      case TUESDAY:
      case WEDNESDAY:
      case THURSDAY:  System.out.println("Working for the man :)");
                      break;

      case FRIDAY:    System.out.println("TGIF ");
                      break;

      case SATURDAY:
      case SUNDAY:    System.out.println("Ahh, the weekend ...");
                      break;

      default:        System.out.println("What day is it?");;
    }
  }
}

/**
 * Our "Day" enum type
 */
enum Day
{
  SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY 
}

When you compile and run this code, the output looks like this:

Ahh, the weekend ...
Working for the man :)
Working for the man :)
Working for the man :)
Working for the man :)
TGIF 
Ahh, the weekend ...

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:

for (Day d: Day.values())
{
  printTodaysThought(d);
}

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: