Skip to main content Skip to docs navigation
TechSpiderTutorials

Understanding Java Enum Types:

Java Enum Types

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.

Enum constants are implicitly static and final, making them immutable and thread-safe by design.
Because they are constants, the names of an enum type's fields are in uppercase letters.

Syntax of Java Enum

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

Benefits of Using Java Enums

  • Readability: Enums enhance code readability by giving meaningful names to constants. For example, using Day.MONDAY is more expressive than using an integer like 1.

  • Type Safety: Since enums are type-safe, the compiler checks their usage at compile-time, reducing the chances of runtime errors. This ensures that only valid enum constants can be assigned to an enum variable.

  • Enumerating Options:Enums are ideal for representing a fixed set of options or choices. For instance, days of the week, card suits (hearts, diamonds, etc.), or HTTP status codes (OK, NOT_FOUND, etc.) are commonly represented using enums.

Using Enums in Java

Enum Constructors and Methods

Enums can have constructors, fields, and methods like any other Java class. Each enum constant can have its own behavior:

public enum Day {
    SUNDAY("Sunday Funday"),
    MONDAY("Monday Blues"),
    TUESDAY("Tuesday Chooseday");

    private final String displayName;

    Day(String displayName) {
        this.displayName = displayName;
    }

    public String getDisplayName() {
        return displayName;
    }
}

Switch Statements with Enums

Enums work seamlessly with switch statements, making code more readable and maintainable:

Day today = Day.MONDAY;

switch (today) {
    case MONDAY:
        System.out.println("Start of the week!");
        break;
    case FRIDAY:
        System.out.println("Weekend is almost here!");
        break;
    // Handle other days
    default:
        System.out.println("Enjoy your day!");
}

Enum Constructors and Methods

// Define an enum called Day to represent days of the week
public enum Day {
    // Enum constants with associated display names
    SUNDAY("Sunday"),
    MONDAY("Monday"),
    TUESDAY("Tuesday"),
    WEDNESDAY("Wednesday"),
    THURSDAY("Thursday"),
    FRIDAY("Friday"),
    SATURDAY("Saturday");

    // Instance variable to hold the display name of each day
    private final String displayName;

    // Constructor for enum constants, initializes the display name
    Day(String displayName) {
        this.displayName = displayName;
    }

    // Method to get the display name of the day
    public String getDisplayName() {
        return displayName;
    }

    // Main method to demonstrate enum usage
    public static void main(String[] args) {
        // Example of using enum constants and methods
        Day today = Day.MONDAY;
        System.out.println("Today is " + today); // Prints: Today is MONDAY
        System.out.println("Display name: " + today.getDisplayName()); // Prints: Display name: Monday
        
        // Example of iterating through all enum constants
        System.out.println("Days of the week:");
        for (Day day : Day.values()) {
            System.out.println(day + ": " + day.getDisplayName());
        }
        
        // Example of using switch statement with enums
        switch (today) {
            case MONDAY:
                System.out.println("It's Monday!");
                break;
            case FRIDAY:
                System.out.println("It's Friday!");
                break;
            // Handle other days
            default:
                System.out.println("It's another day of the week.");
        }
    }
}

Output:

Today is MONDAY
Display name: Monday
Days of the week:
SUNDAY: Sunday
MONDAY: Monday
TUESDAY: Tuesday
WEDNESDAY: Wednesday
THURSDAY: Thursday
FRIDAY: Friday
SATURDAY: Saturday
It's Monday!