Java Enum Constructors
In Java, enums (short for enumerations) are a powerful feature that allows you to define a fixed set of constants. One of the most useful aspects of enums is the ability to use constructors to associate specific data or behavior with each constant. In this guide, we’ll dive deep into Java enum constructors—how to define them, when to use them, and practical examples.
In Java, an enum constructor allows you to assign additional data to each enum constant. Every enum constant can be initialized with values passed to the constructor, which can help encapsulate information associated with each constant.
Enum constructors can be useful when you need to associate specific values or behaviors with each constant, for instance:
The syntax for defining a constructor inside an enum is similar to regular Java class constructors. However, the enum constructor is private by default, which means it cannot be invoked from outside the enum class.
Here is the general syntax:
public enum EnumName {
CONSTANT_NAME(parameter1, parameter2);
// Instance fields
private final Type1 field1;
private final Type2 field2;
// Constructor
private EnumName(Type1 field1, Type2 field2) {
this.field1 = field1;
this.field2 = field2;
}
}
Let’s consider an example where we represent a Planet
enum with a constructor that accepts parameters for the planet’s mass and radius.
public enum Planet {
MERCURY(3.3011e23, 2439.7),
VENUS(4.8675e24, 6051.8),
EARTH(5.97237e24, 6371),
MARS(6.4171e23, 3389.5);
private final double mass; // in kilograms
private final double radius; // in kilometers
// Constructor
private Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
public double getMass() {
return mass;
}
public double getRadius() {
return radius;
}
}
Now you can access the mass and radius of each planet using getter methods:
public class EnumConstructorExample {
public static void main(String[] args) {
for (Planet planet : Planet.values()) {
System.out.println(planet + ": Mass = " + planet.getMass() + " kg, Radius = " + planet.getRadius() + " km");
}
}
}
Output:
MERCURY: Mass = 3.3011E23 kg, Radius = 2439.7 km
VENUS: Mass = 4.8675E24 kg, Radius = 6051.8 km
EARTH: Mass = 5.97237E24 kg, Radius = 6371.0 km
MARS: Mass = 6.4171E23 kg, Radius = 3389.5 km
You can also have multiple constructors for enums, which can allow you to pass different sets of parameters for each constant. Java enum constructors are just like regular constructors, but you cannot have multiple constructors with the same parameters for different constants. However, you can overload constructors to provide flexibility in instantiation.
In this example, let's add a constructor that handles different types of planets:
public enum Planet {
MERCURY(3.3011e23, 2439.7, "Terrestrial"),
VENUS(4.8675e24, 6051.8, "Terrestrial"),
EARTH(5.97237e24, 6371, "Terrestrial"),
MARS(6.4171e23, 3389.5, "Terrestrial"),
JUPITER(1.8982e27, 69911, "Gas Giant"),
SATURN(5.6834e26, 58232, "Gas Giant");
private final double mass; // in kilograms
private final double radius; // in kilometers
private final String type; // Type of planet (Terrestrial or Gas Giant)
// Constructor for terrestrial planets
private Planet(double mass, double radius, String type) {
this.mass = mass;
this.radius = radius;
this.type = type;
}
public double getMass() {
return mass;
}
public double getRadius() {
return radius;
}
public String getType() {
return type;
}
}
You can now easily query the planet type for each enum constant:
public class EnumMultipleConstructors {
public static void main(String[] args) {
for (Planet planet : Planet.values()) {
System.out.println(planet + " is a " + planet.getType() + " planet.");
}
}
}
Output:
MERCURY is a Terrestrial planet.
VENUS is a Terrestrial planet.
EARTH is a Terrestrial planet.
MARS is a Terrestrial planet.
JUPITER is a Gas Giant planet.
SATURN is a Gas Giant planet.
Here are some tips to help you use Java enum constructors effectively: