← 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.

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
enum Level {
	LOW(1), MEDIUM(2), HIGH(3);
	int code;
	Level(int code) { this.code = code; }
}
public class Main {
	public static void main(String[] args) {
		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);
	}
}

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.