Q:

Java program to create the main() method inside the enum class

belongs to collection: Java Enums Programs

0

Java program to create the main() method inside the enum class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

In this program, we will create a vehicle enumeration class using "enum" that contains the main() method and access the created enum constants.

Program/Source Code:

The source code to create the main() method inside the enum class is given below. The given program is compiled and executed successfully.

// Java program to create the main() method 
// inside the enum class

public enum Vehicle {
  BIKE,
  CAR,
  BUS;

  public static void main(String[] args) {
    Vehicle car = Vehicle.CAR;
    System.out.println("Vehicle is: " + car);
  }
}

Output:

Vehicle is: CAR

Explanation:

In the above program, we created an enumeration class with constants and the main() method. Here, we created the object of enumeration class Vehicle and initialized it with CAR constant and printed the result.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to access enum constants using \'... >>
<< Java program to demonstrate an enum in switch case...