Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – enum in switch statement

We can use enumeration value in the switch statement.

Example

 enum Fruits 
 {
     Apple, Banana, Pineapple, Papaya, Strawberry
 }

 class SwitchCaseDemo 
 {
     public static void main(String args[])
     {
         Fruits f;
         f = Fruits.Pineapple;
         switch(f)
         {
             case Apple:
                    System.out.println("Apple");
                    break;
             case Banana:
                    System.out.println("Banana");
                    break;
             case Pineapple:
                    System.out.println("Pineapple");
                    break;
             case Papaya:
                    System.out.println("Papaya");
                    break;
             default:
                    System.out.println("Match not found");
         }
        
         System.out.println("---------------------------");
        
         switch(Fruits.Strawberry)
         {
             case Apple:
                    System.out.println("Apple");
                    break;
             case Banana:
                    System.out.println("Banana");
                    break;
             case Pineapple:
                    System.out.println("Pineapple");
                    break;
             case Strawberry:
                    System.out.println("Strawberry");
                    break;
             default:
                    System.out.println("Match not found");
         }
     }
 }

Output

Pineapple
---------------------------
Strawberry

 

Leave a reply