Java Enum Constructor

 

In Java, an enum is much more powerful than a simple list of constants. Each enum constant can have its own values and behaviors—thanks to the ability to define constructors, fields, and methods inside an enum.

However, enum constructors behave differently from normal class constructors. Let’s explore how they work.

In this guide, we’ll cover:

  • What enum constructors are
  • Rules for enum constructors
  • Examples of enum constructors
  • Best practices

What Is an Enum Constructor?

 
  • An enum constructor initializes each enum constant.
  • Like regular classes, enums can have fields and constructors.
  • Enum constructors are executed automatically when constants are created.

Rules for Enum Constructors

 
  1.  Enum constructors are always private (or package-private).
    • You cannot declare them as public or protected.
    • You cannot call them explicitly using new.
  2. Each enum constant must call one of the constructors.
  3. Enum constructors run once per constant when the enum class is loaded.

Example 1: Enum Constructor with One Field

 

public enum Day {
  MONDAY(“Start of the week”),
  FRIDAY(“Weekend is near”),
  SUNDAY(“Holiday”);

  private String description;

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

  public String getDescription() {
    return description;
  }
}

public class EnumConstructorExample {
  public static void main(String[] args) {
    for (Day d : Day.values()) {
      System.out.println(d + ” → ” + d.getDescription());
    }
  }
}

Output:

MONDAY → Start of the week
FRIDAY → Weekend is near
SUNDAY → Holiday

Example 2: Enum Constructor with Multiple Fields

 

public enum TrafficLight {
  RED(30, “Stop”),
  YELLOW(5, “Get Ready”),
  GREEN(25, “Go”);

  private int duration;
  private String action;

  // constructor
  TrafficLight(int duration, String action) {
    this.duration = duration;
    this.action = action;
  }

  public int getDuration() { return duration; }
  public String getAction() { return action; }
}

public class TrafficLightTest {
  public static void main(String[] args) {
    for (TrafficLight t : TrafficLight.values()) {
      System.out.println(t + ” → ” + t.getAction() + ” for ” + t.getDuration() + ” seconds.”);
    }
  }
}

Output:

RED → Stop for 30 seconds.
YELLOW → Get Ready for 5 seconds.
GREEN → Go for 25 seconds.

Example 3: Enum Constructor with Method Override

 

Enums can also override methods for individual constants.

public enum Operation {
  ADD(“+”) {
    public int apply(int a, int b) { return a + b; }
  },
  MULTIPLY(“*”) {
    public int apply(int a, int b) { return a * b; }
  };

  private String symbol;

  Operation(String symbol) { this.symbol = symbol; }

  public String getSymbol() { return symbol; }

  public abstract int apply(int a, int b);
}

public class OperationTest {
  public static void main(String[] args) {
    System.out.println(Operation.ADD.apply(5, 3)); // 8
    System.out.println(Operation.MULTIPLY.apply(5, 3)); // 15
  }
}

Best Practices

 
  • Use enum constructors to assign descriptive values to constants.
  • Keep constructors private (enforced by Java).
  • Avoid putting complex logic in enum constructors—keep them simple.
  • Prefer enums over public static final constants for type safety.

Conclusion

 

Java enum constructors make enums far more powerful than simple constants.

  • Constructors are private and cannot be called directly.
  • Each enum constant calls a constructor when created.
  • Enums can have fields, methods, and overridden behaviors.

By leveraging enum constructors, you can create enums that are type-safe, flexible, and expressive.

Scroll to Top