Java Enum Constructor: Understanding Enums in Java

Explore Java enums, a special class type representing a group of predefined constant values. Learn how to define and utilize enums in your applications, including their use in switch expressions and as constants. Discover how enum constructors work and see practical examples to enhance your understanding of this essential Java feature.



Java - Enum Constructor

What is an Enum in Java?

Java enum is a special kind of class that represents a group of pre-defined constant values. Enums can be used in switch expressions for comparison and as constants in application code.

By default, an enum does not require any constructor, and its default value is the same as its declaration. Consider the following example:

WEEKDAY Enum Example

enum WEEKDAY { 
MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY, SATURDAY, SUNDAY 
}

If we print any of the values of the above enum, it will output the same string as declared:

Print Enum Value

System.out.println(WEEKDAY.MONDAY);

Output


MONDAY

Use of Enum Constructor

If we want to assign a default value to an enum, we can create an enum constructor as shown below:

WEEKDAY Enum with Constructor

enum WEEKDAY {
MONDAY("Day 1");
private final String description;

WEEKDAY(String description) {
    this.description = description;
}
}

In this case, we're assigning a default description to the enum using the constructor.

Scope of Enum Constructor

As enums are a special kind of class, an enum constructor can only have a private or package-private modifier. Setting a public identifier for an enum constructor will result in a compile-time error:

Invalid Enum Constructor Example

enum WEEKDAY {
MONDAY("Day 1");
private final String description;

// Compiler error: illegal modifier, only private is permitted.
public WEEKDAY(String description) {
    this.description = description;
}
}

Valid Declarations of Enum Constructor

Here are valid declarations of enum constructors:

Enum with Private Constructor

Private Constructor Example

enum WEEKDAY {
MONDAY("Day 1");
private final String description;

// Valid declaration
private WEEKDAY(String description) {
    this.description = description;
}
}

Example: Enum with Private Constructor

In this example, we've created an enum WEEKDAY with a private constructor. Using this constructor, we're setting the value of the enum description:

WEEKDAY Enum Example

package com.tutorialsarena;

// Create the enum
enum WEEKDAY {
// Create values of enum
MONDAY("Day 1"),
TUESDAY("Day 2"),
WEDNESDAY("Day 3"),
THRUSDAY("Day 4"),
FRIDAY("Day 5"),
SATURDAY("Day 6"),
SUNDAY("Day 7");

private final String description;

// Private constructor to set default value
private WEEKDAY(String description) {
    this.description = description;
}

// Getter method to get the description
public String getDescription() {
    return this.description;
}
}

public class Tester {
public static void main(String[] args) {
    System.out.println(WEEKDAY.MONDAY.getDescription());
    System.out.println(WEEKDAY.MONDAY);
}
}

Output


Day 1
MONDAY

Enum with Package-private Constructor

We can create a package-private constructor for an enum as shown below:

Package-private Constructor Example

enum WEEKDAY {
MONDAY("Day 1");
private final String description;

// Valid declaration
WEEKDAY(String description) {
    this.description = description;
}
}

Example: Enum with Package-private Constructor

In this example, we've created an enum WEEKDAY with a package-private constructor, allowing us to set the value of the enum description:

WEEKDAY Enum Example

package com.tutorialsarena;

// Create the enum
enum WEEKDAY {
// Create values of enum
MONDAY("Day 1"),
TUESDAY("Day 2"),
WEDNESDAY("Day 3"),
THRUSDAY("Day 4"),
FRIDAY("Day 5"),
SATURDAY("Day 6"),
SUNDAY("Day 7");

private final String description;

// Package-private constructor to set default value
WEEKDAY(String description) {
    this.description = description;
}

// Getter method to get the description
public String getDescription() {
    return this.description;
}
}

public class Tester {
public static void main(String[] args) {
    System.out.println(WEEKDAY.MONDAY.getDescription());
    System.out.println(WEEKDAY.MONDAY);
}
}

Output


Day 1
MONDAY