0
0
JavaHow-ToBeginner · 3 min read

How to Add Constructor to Enum in Java: Simple Guide

In Java, you add a constructor to an enum by defining it like a class constructor inside the enum body. Each enum constant can then pass arguments to this constructor to initialize fields.
📐

Syntax

An enum constructor looks like a normal class constructor but is always private or package-private. You define fields to hold data, then create a constructor to set those fields. Each enum constant calls this constructor with its own values.

java
public enum Day {
    MONDAY("Start of work week"),
    TUESDAY("Second day"),
    WEDNESDAY("Midweek"),
    THURSDAY("Almost Friday"),
    FRIDAY("End of work week"),
    SATURDAY("Weekend!"),
    SUNDAY("Weekend!");

    private final String description;

    // Constructor
    Day(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }
}
💻

Example

This example shows an enum Day with a constructor that takes a description. Each day passes its own description. The getDescription() method returns the stored text.

java
public class EnumConstructorExample {
    public enum Day {
        MONDAY("Start of work week"),
        TUESDAY("Second day"),
        WEDNESDAY("Midweek"),
        THURSDAY("Almost Friday"),
        FRIDAY("End of work week"),
        SATURDAY("Weekend!"),
        SUNDAY("Weekend!");

        private final String description;

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

        public String getDescription() {
            return description;
        }
    }

    public static void main(String[] args) {
        for (Day day : Day.values()) {
            System.out.println(day + ": " + day.getDescription());
        }
    }
}
Output
MONDAY: Start of work week TUESDAY: Second day WEDNESDAY: Midweek THURSDAY: Almost Friday FRIDAY: End of work week SATURDAY: Weekend! SUNDAY: Weekend!
⚠️

Common Pitfalls

  • Forgetting the constructor is private: Enum constructors cannot be public or protected; they are implicitly private.
  • Missing semicolon after enum constants: When you add fields or methods, you must end the list of constants with a semicolon.
  • Not passing arguments to constructor: Each enum constant must call the constructor with the correct parameters.
java
public enum Color {
    RED("#FF0000"),
    GREEN("#00FF00"),
    BLUE("#0000FF"); // Added missing semicolon here

    private final String hex;

    Color(String hex) {
        this.hex = hex;
    }

    public String getHex() {
        return hex;
    }
}

// Correct version adds semicolon after BLUE("#0000FF");
📊

Quick Reference

  • Enum constructors are always private or package-private.
  • Enum constants must call the constructor with matching arguments.
  • End enum constants list with a semicolon if you add fields or methods.
  • Use getters to access enum fields.

Key Takeaways

Enum constructors in Java are private and initialize fields for each constant.
Each enum constant must call the constructor with the correct parameters.
Always put a semicolon after enum constants if you add fields or methods.
Use getter methods to access data stored in enum fields.
Enum constructors cannot be public or protected.