Write a Java Program to Create an enum class

In Java, an enum is a special data type that is used to define a set of named constants.

It’s often used to represent a fixed number of choices or options in a program.

An enum is declared using the enum keyword followed by the name of the enum class, which contains a list of constants enclosed in curly braces.


To create an enum class in Java, you can follow these steps:

  1. Declare the enum class using the “enum” keyword, followed by the name of the enum class. For example:
public enum DayOfWeek {
   MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
  1. Declare the constants within the enum class, separated by commas. Each constant should be named in uppercase letters. For example:
public enum DayOfWeek {
   MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
  1. Access the constants using the enum class name followed by the constant name. For example:
DayOfWeek today = DayOfWeek.TUESDAY;

Here’s a full example of an enum class in Java:

public class EnumExample {
   public enum DayOfWeek {
      MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
   }

   public static void main(String[] args) {
      DayOfWeek today = DayOfWeek.TUESDAY;
      System.out.println("Today is " + today);
   }
}

In this example, the DayOfWeek enum class is declared and contains the constants for each day of the week.

The main method creates an instance of the enum class and prints out the current day.


Enums are a useful tool in Java programming as they help make code more readable and maintainable.

They also provide a way to represent a fixed set of options or choices in a program.

By using enums, you can avoid hard-coding string or numeric values in your code, which can lead to errors and make code harder to maintain.