🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #80

Enum Constructor


Enum Constructor

An enum can also have a constructor just like a class.

The constructor is called automatically when the constants are created. You cannot call it yourself.

Here, each constant in the enum has a value (a string) that is set through the constructor:

enum Level {
  // Enum constants (each has its own description)
  LOW("Low level"),
  MEDIUM("Medium level"),
  HIGH("High level");

  // Field (variable) to store the description text
  private String description;

  // Constructor (runs once for each constant above)
  private Level(String description) {
    this.description = description;
  }

  // Getter method to read the description
  public String getDescription() {
    return description;
  }
}

public class Main {
  public static void main(String[] args) {
    Level myVar = Level.MEDIUM; // Pick one enum constant
    System.out.println(myVar.getDescription()); // Prints "Medium level"
  }
}

Note: The constructor for an enum must be private. If you don't write private, Java adds it automatically.


Loop Through Enum with Constructor

You can also loop through the constants and print their values using the values() method:

for (Level myVar : Level.values()) {
  System.out.println(myVar + ": " + myVar.getDescription());
}

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Enum Constructor – FAQs

Quick answers about learning Enum Constructor in Java.

This free note from CodingNow 2.0 explains Enum Constructor in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Enum Constructor, is 100% free with no signup required.
With focused practice, most students grasp Enum Constructor in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now