In this program, we will create a vehicle enumeration using enum inside the class Main. Then we will access the enum constant inside the main() method and print it.
Program/Source Code:
The source code to create an enum inside the class is given below. The given program is compiled and executed successfully.
// Java program to create an enum
// inside the class
public class Main {
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 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 an enum object and initialized it with CAR constant. After that, we printed the enum constant.
In this program, we will create a vehicle enumeration using enum inside the class Main. Then we will access the enum constant inside the main() method and print it.
Program/Source Code:
The source code to create an enum inside the class is given below. The given program is compiled and executed successfully.
Output:
Explanation:
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 an enum object and initialized it with CAR constant. After that, we printed the enum constant.