ADVERTISEMENT
ADVERTISEMENT

Java program to create a simple enum

In this example, we will learn to create a simple enum in Java.
Submitted by Nidhi, on April 04, 2022

Problem Solution:

In this program, we will create a vehicle enumeration using "enum". Then we will access enum constant and print.

Program/Source Code:

The source code to create a simple enum is given below. The given program is compiled and executed successfully.

// Java program to create a simple enum

enum Vehicle {
  BIKE,
  CAR,
  BUS
}

public class Main {
  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 and a class Main. The enum Vehicle contains 3 constants BIKE, CAR, BUS. The Main class contains a static method main(). The main() method is the entry point for the program, here we created an object of enum and initialized it with CAR constant. After that, we printed the enum constant.

Java Enums Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.