Java Program to Demonstrate an Enum in Switch Case

You are currently viewing Java Program to Demonstrate an Enum in Switch Case

Java program to demonstrate an enum in switch case

// Java program to demonstrate an enum 
// in switch case

public class Main {
  enum Vehicle {
    BIKE,
    CAR,
    BUS
  }

  public static void main(String[] args) {
    String str = "BUS";

    switch (Vehicle.valueOf(str)) {
    case BIKE:
      System.out.println("BIKE is for 2 persons.");
      break;

    case CAR:
      System.out.println("CAR is for 5 persons.");
      break;

    case BUS:
      System.out.println("BUS is for 50 persons.");
      break;

    default:
      System.out.println("Unknown Vehicle.");
      break;
    }
  }
}

Output

BUS is for 50 persons.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply