← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 5 of 10

Java Enum Constructor

Enums can define a constructor, called automatically once per constant, letting each constant carry its own data through fields set at construction time.
Syntax
java
enum EnumName {
    CONSTANT1(value1), CONSTANT2(value2);

    private final dataType field;
    EnumName(dataType field) { this.field = field; }
}

Enums Can Have Constructors

Like a regular class, an enum can define a constructor, and Java automatically calls that constructor once for every constant listed at the top of the enum, at the moment the enum type is first loaded.

Example: Enums Can Have Constructors

java
enum Level {
	LOW, MEDIUM, HIGH;
	Level() {
		System.out.println("Constant created: " + this);
	}
}
public class Main {
	public static void main(String[] args) {
		Level l = Level.LOW; // constructor runs for every constant at class load
	}
}

Passing Values to Enum Constants

Each enum constant can supply its own arguments to the shared constructor by writing them in parentheses after the constant's name, letting every constant carry a different associated value.

Example: Passing Values to Enum Constants

java
// Define the enum Level (a fixed set of constants)
enum Level {
	LOW(1), MEDIUM(2), HIGH(3);
	int code;
	Level(int code) { this.code = code; }
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(Level.HIGH.code);
	}
}

Adding Fields to an Enum

Fields declared inside an enum are typically set by the constructor and marked final, giving every constant its own permanent piece of data, such as a numeric code, a display string, or a measurement.

Example: Adding Fields to an Enum

java
enum Planet {
	EARTH(5.97), MARS(0.64);
	final double mass; // set by constructor, permanent per constant
	Planet(double mass) { this.mass = mass; }
}
public class Main {
	public static void main(String[] args) {
		System.out.println(Planet.MARS.mass);
	}
}

Adding Methods to an Enum

An enum can also define regular methods, just like any other class, and those methods can read the fields that were set by the constructor to compute or return information specific to each constant.

Example: Adding Methods to an Enum

java
enum Planet {
	EARTH(5.97), MARS(0.64);
	final double mass;
	Planet(double mass) { this.mass = mass; }
	double massInKg() {
		return mass * 1e21; // uses the field set by the constructor
	}
}
public class Main {
	public static void main(String[] args) {
		System.out.println(Planet.EARTH.massInKg());
	}
}

Enum Constructors are Always Private

An enum's constructor is always implicitly private, even if no access modifier is written, because the language itself guarantees the only instances that can ever exist are the fixed set of constants declared in the enum.

Example: Enum Constructors are Always Private

java
enum Level {
	LOW, HIGH;
	Level() { // implicitly private, even without writing it
	}
}
public class Main {
	public static void main(String[] args) {
		// new Level(); // would not compile: constructor is private
		System.out.println(Level.LOW);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Marking the constructor public, which is a compile error.
  2. Calling the enum constructor with new.
  3. Forgetting to define the field and a matching constructor, so the values are not stored.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.