×

Java Programs

Java Practice

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

Java example to create the main() method inside the enum class.
Submitted by Nidhi, on April 04, 2022

Problem Solution:

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.

Java Enums Programs »





Comments and Discussions!

Load comments ↻





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