Java Data Type How to - Switch with enum








Question

We would like to know how to switch with enum.

Answer

public class MainClass {
  enum Choice { Choice1, Choice2, Choice3 }
  public static void main(String[] args) {
    Choice ch = Choice.Choice1;//from   w ww .j ava  2  s .  co  m

    switch(ch) {
      case Choice1:
        System.out.println("Choice1 selected");
        break;
     case Choice2:
       System.out.println("Choice2 selected");
       break;
     case Choice3:
       System.out.println("Choice3 selected");
       break;
    }
  }
}

The code above generates the following result.