Java Enum Class: Creating a New Enum

Enum in Java is a special data type that allows a variable to be a set of predefined constants. It provides a way to create a group of named constants that are more meaningful and easier to read in the code. Enum can be used to represent a fixed number of predefined values.

Enum Class Structure

An enum class in Java is declared using the enum keyword followed by the class name and a list of comma-separated constants enclosed in curly braces. Each constant represents an instance of the enum class.

Here's a basic example of defining an enum class called Color with three constants: RED, GREEN, and BLUE.

public enum Color {
    RED,
    GREEN,
    BLUE
}

Creating a New Enum

To create a new enum class, you can follow these steps:

  1. Define a new enum class with the desired constants.
  2. Add any necessary fields or methods to the enum class.

Let's create a new enum class called Day that represents the days of the week with additional information such as the abbreviation and the ordinal value.

public enum Day {
    MONDAY("Mon", 1),
    TUESDAY("Tue", 2),
    WEDNESDAY("Wed", 3),
    THURSDAY("Thu", 4),
    FRIDAY("Fri", 5),
    SATURDAY("Sat", 6),
    SUNDAY("Sun", 7);

    private final String abbreviation;
    private final int ordinalValue;

    Day(String abbreviation, int ordinalValue) {
        this.abbreviation = abbreviation;
        this.ordinalValue = ordinalValue;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public int getOrdinalValue() {
        return ordinalValue;
    }
}

In this example, we have defined an enum class Day with constants for each day of the week along with additional fields for abbreviation and ordinal value. The constructor initializes these fields for each constant, and getter methods are provided to access the values.

Using the New Enum

Once the enum class is defined, you can use it in your Java code to represent and work with the predefined constants. Here's an example of how to use the Day enum:

public class EnumExample {
    public static void main(String[] args) {
        Day today = Day.MONDAY;
        System.out.println("Today is " + today);
        System.out.println("Abbreviation: " + today.getAbbreviation());
        System.out.println("Ordinal value: " + today.getOrdinalValue());
    }
}

When you run the EnumExample class, it will output:

Today is MONDAY
Abbreviation: Mon
Ordinal value: 1

State Diagram

stateDiagram
    [*] --> MONDAY: 1
    MONDAY --> TUESDAY: 2
    TUESDAY --> WEDNESDAY: 3
    WEDNESDAY --> THURSDAY: 4
    THURSDAY --> FRIDAY: 5
    FRIDAY --> SATURDAY: 6
    SATURDAY --> SUNDAY: 7
    SUNDAY --> [*]

In the state diagram, each enum constant represents a state with transitions between them based on the ordinal values.

Conclusion

In this article, we have explored how to create a new enum class in Java by defining constants with additional fields and methods. Enums are a powerful feature in Java that provides a convenient way to represent a fixed set of values. By following the steps outlined here, you can easily create and use enum classes to improve the readability and maintainability of your code.