30% Therapy – 40% Practice – 30% Work project

Java – Enum Constructor



is a special kind of that represents a group of pre-defined constant values and can be used in for comparison, to be used 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:

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

If we print the any of the value of above enum, it will print the same string as declared.

System.out.println(WEEKDAY.MONDAY);

It will print the result as shown below:

MONDAY

Use of Enum Constructor

Now in case, we want to assign a default value to enum, we can create a enum constructor as shown below:

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 enum is a special class, an enum constructor can only have private or package-private modifier. Setting a public idenifier to enum constructor will attract a compile time error.

enum WEEKDAY {
	MONDAY("Day 1");
	private final String description;
	
	// compiler error: illegal modifier, only private is permitted. 
	public WEEKDAY(String description) {
		this.description = description;
	}
}

Following are valid declarations of enum constructor

Enum with Private Constructor

We can create a private constructor for an enum as shown below.

enum WEEKDAY {
	MONDAY("Day 1");
	private final String description;
	
	// valid declaration
	private WEEKDAY(String description) {
		this.description = description;
	}
}

Example

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

package com.tutorialspoint;

// 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 construtor 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);
   }
}

Let us compile and run the above program, this will produce the following result −

Day 1
MONDAY

Enum with Package-private Constructor

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

enum WEEKDAY {
	MONDAY("Day 1");
	private final String description;
	
	// valid declaration
    WEEKDAY(String description) {
		this.description = description;
	}
}

Example

In this example, we”ve created an enum WEEKDAY with a package-private constructor as no modifier is passed. Using this constructor, we”re setting the value of enum description using its constructor.

package com.tutorialspoint;

// 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 construtor 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);
   }
}

Let us compile and run the above program, this will produce the following result −

Day 1
MONDAY
Translate »