Java Programs

Java Practice

Java program to demonstrate an enum in switch case

Java example to demonstrate an enum in switch case.
Submitted by Nidhi, on April 04, 2022

Problem statement

In this program, we will create a vehicle enumeration using "enum" inside the class Main. Then we will use an enum constant with a switch case inside the main() method and print the appropriate message.

Java program to demonstrate an enum in switch case

The source code to demonstrate an enum in the switch case is given below. The given program is compiled and executed successfully.

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

public class Main {
  enum Vehicle {

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

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

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

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

      System.out.println("Unknown Vehicle.");


BUS is for 50 persons.


In the above program, we created an enumeration Vehicle inside class Main. The enum Vehicle contains 3 constants BIKE, CAR, BUS. The Main class also contains a static method main(). The main() method is the entry point for the program, here we created a string initialized with "BUS". Then we converted the string into an enum constant using the valueOf() method and used it in a switch case and printed the appropriate message.

Java Enums Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.