Back to Java
2026-03-275 min read

Enum Constructor

Learn Enum Constructor step by step with clear examples and exercises.

Title: Java Enum Constructor - A full guide

Why This Matters

In Java, an enum (enumeration) is a special data type that represents a set of constants. One of the key features of enums is their constructors, which allow for customization of instances when they are created. Understanding how to use enum constructors can help you create more efficient and flexible code in your Java projects.

Prerequisites

Before diving into the details of enum constructors, it's essential that you have a solid understanding of the following topics:

  • Basic Java syntax and data types
  • Classes and objects
  • Interfaces
  • Static methods and variables
  • Access modifiers (public, private, protected, default)

Core Concept

An enum in Java is a special type of class that can only have constant values. To create an enum, you define a new enum type with the enum keyword followed by the enum name. Each enum value should be listed as a constant within curly braces. Here's an example:

public enum Days {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

By default, each enum value is automatically given a public, static, and final modifier. You can also provide a constructor for your enum to customize its instances. To define a constructor, use the enum keyword followed by the enum name and then curly braces containing the constructor definition.

public enum Days {
MONDAY("Monday"), TUESDAY("Tuesday"), WEDNSEDAY("Wednesday");

private final String dayName;

Days(String dayName) {
this.dayName = dayName;
}
}

In the example above, we've added a constructor to our enum that takes a String argument and assigns it to a private field called dayName. This allows us to customize the name of each day when creating an instance. You can now access the dayName for any given day using the dot notation:

Days today = Days.MONDAY;
System.out.println(today.dayName); // Output: Monday

Worked Example

Let's create an enum to represent different shapes and provide a constructor that calculates the area of each shape based on its dimensions.

public enum Shape {
RECTANGLE(4, 5), CIRCLE(3), SQUARE(5);

private final double dimension1;
private final double dimension2;

Shape(double dimension1, double dimension2) {
this.dimension1 = dimension1;
this.dimension2 = dimension2;
}

public double getArea() {
switch (this) {
case RECTANGLE:
return dimension1 * dimension2;
case CIRCLE:
return Math.PI * Math.pow(dimension1 / 2, 2);
case SQUARE:
return Math.pow(dimension1, 2);
}
}
}

In this example, we've defined an enum called Shape with three constants representing different shapes (RECTANGLE, CIRCLE, and SQUARE). Each constant has a constructor that takes dimensions as arguments. We also added a getArea() method to calculate the area of each shape based on its dimensions.

Common Mistakes

  1. Forgetting to provide a constructor for your enum - If you don't provide a constructor, Java will automatically create a default one that takes no arguments. However, this might not be suitable for your needs, so it's essential to define a custom constructor when necessary.
  2. Not initializing private fields in the constructor - Make sure to initialize all private fields within the constructor to avoid null pointer exceptions or unexpected behavior.
  3. Misusing enum constructors - Remember that enum constructors are called when an instance of the enum is created, not when the enum class itself is loaded. This means you can't use enum constructors for static initialization or one-time setup tasks.
  4. Not making the constructor private - By default, enum constructors are private, but it's still a good practice to explicitly declare them as such to prevent accidental subclassing of your enum.
  5. Forgetting to use this when initializing private fields - When you initialize private fields within the constructor, make sure to use the this keyword to reference the current instance.

Practice Questions

  1. Create an enum called Fruit with a constructor that takes a name and color as arguments. Add a method called getDetails() that returns a string containing the fruit's name and color.
  2. Modify the Shape example to include a third shape (TRIANGLE) with dimensions for base and height. Calculate the area using the formula 0.5 base height.
  3. Create an enum called Animal with constructors that take different arguments based on the animal type (e.g., cats have names, dogs have names and breeds). Add a method called makeSound() that returns a string representing the animal's sound.

FAQ

  1. Can I define multiple constructors for an enum?
  • Yes, you can define multiple constructors for an enum by providing overloaded constructor methods with different arguments. However, be aware that only one constructor will be called when creating an instance of the enum.
  1. What happens if I don't provide a constructor for my enum?
  • If you don't provide a constructor for your enum, Java will automatically create a default constructor that takes no arguments. This might not be suitable for your needs, so it's essential to define a custom constructor when necessary.
  1. Can I call methods on an enum constant directly?
  • No, you cannot call methods on an enum constant directly. Instead, you need to create an instance of the enum and then call the method on that instance.
  1. How do I access the private fields within an enum constructor?
  • To access the private fields within an enum constructor, use the this keyword to reference the current instance.
  1. Can I override methods in an enum?
  • Yes, you can override methods in an enum just like any other class. However, keep in mind that overriding methods in an enum doesn't provide polymorphic behavior since enum constants are not instances of their respective classes.
Enum Constructor | Java | XQA Learn