Java - Enum Type in switch Statements

Introduction

You can use enum types in switch statements.

When the switch expression is using an enum type, all case labels must be unqualified enum constants of the same enum type.

Demo

enum Level {
  LOW, MEDIUM, HIGH, URGENT;//  w  ww. ja  v a2  s  .  c om
}

public class Main {
  public static void main(String[] args) {
    Level s1 = Level.LOW;
    Level s2 = Level.HIGH;

    System.out.println(getValue(Level.LOW));
    System.out.println(getValue(s2));
  }

  public static int getValue(Level severity) {
    int days = 0;
    switch (severity) {
    // Must use the unqualified name LOW, not Level.LOW
    case LOW:
      days = 30;
      break;
    case MEDIUM:
      days = 15;
      break;
    case HIGH:
      days = 7;
      break;
    case URGENT:
      days = 1;
      break;
    }

    return days;
  }
}

Result

Quiz