A Java enum switch/case statement example

Java enum FAQ: Can you share a Java enum switch example? That is, 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 a Java enum with a variety of Java constructs, including a Java switch statement, a for loop, and an if/then statement.

In this shorter enum tutorial, I want to just focus on using an enum in a switch statement. Hopefully this Java enum switch statement example adds a little more complexity to my earlier example.

A Java enum switch statement example

In this Java enum switch example, I declare a Java 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 this enum example, and then I'll describe it afterwards:

/**
 * A Java enum switch statement (switch/case) example.
 * @author alvin alexander, devdaily.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 Java enum class, 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.

Java enum switch example - discussion

As with any Java program, the flow of control starts in our main method. In the main method, we jump right in with this enum for loop code:

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

This "enum for loop" iterates through all the values in our Day enum, using the values method that comes with the Java enum type. That's really the only "trick" in this code; the rest of this 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 our Java switch statement. For the values MONDAY through THURSDAY we print one String; for FRIDAY we 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 we add a new value to the Day enum -- flow of control will fall down to our default expression.

Java enum switch example - Summary

I hope this Java enum switch statement example has been helpful. Between my original Java enum tutorial and this "enum switch tutorial", I hope it has helped to see at least two examples of how to use a Java enum type with a Java switch statement (sometimes called a Java case statement).

If you have any questions or comments, please use the comment form below, and I'll get back with you.

Java enum - What's Related

As I finish up my Java enum series, here's a collection of the Java enum tutorials I've put together during the last week. I hope you find them helpful:

Beware of nulls

Hi,

Thank you for the nice example. Please note that in "switch (theDay)", of theDay is null, you'll get a NullPointerException. Yes, enums are objects, so a variable of enum type may be null. You might have expected that null would fall under the "default" case; not so.

Ole

Post new comment

The content of this field is kept private and will not be shown publicly.
<