Use of java enum in switch case statement

Let’s learn the use of java enum in switch case statement.

Use of java enum in switch case statement

Enum types are special data type that enables variable to be a set of predefined constants.

Here’s java program on enum and switch statement.

enum Fruit
{
   apple, mango, jackfruit, orange, grapes
}
public class SwitchEnumDemo
{
   public static void main(String[] args)
   {
      Fruit f;
      f = Fruit.mango;
      switch (f) {
          case apple:
              System.out.println("You have selected apple.");
              break;
          case mango:
              System.out.println("You have selected mango.");
              break;
          case jackfruit:
              System.out.println("You have selected jackfruit.");
              break;
          case orange:
              System.out.println("You have selected orange.");
              break;
          case grapes:
              System.out.println("You have selected grapes.");
              break;
          default:
              System.out.println("Invalid selection!");
              break;
      }
   }
}


Output:

You have selected mango.


Also read – java overview