Java – Enum Constructor

Java enum is a special kind of class that represents a group of pre-defined constant values and can be used in switch expressions 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:

enumWEEKDAY{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:

enumWEEKDAY{MONDAY("Day 1");privatefinalString 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.

enumWEEKDAY{MONDAY("Day 1");privatefinalString description;// compiler error: illegal modifier, only private is permitted. publicWEEKDAY(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.

enumWEEKDAY{MONDAY("Day 1");privatefinalString description;// valid declarationprivateWEEKDAY(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.

packagecom.tutorialspoint;// create the enumenumWEEKDAY{// create values of enumMONDAY("Day 1"),TUESDAY("Day 2"),WEDNESDAY("Day 3"),THRUSDAY("Day 4"),FRIDAY("Day 5"),SATURDAY("Day 6"),SUNDAY("Day 7");privatefinalString description;// private construtor to set default valueprivateWEEKDAY(String description){this.description = description;}// getter method to get the descriptionpublicString getDescription (){returnthis.description;}}publicclassTester{publicstaticvoidmain(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.

enumWEEKDAY{MONDAY("Day 1");privatefinalString description;// valid declarationWEEKDAY(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.

packagecom.tutorialspoint;// create the enumenumWEEKDAY{// create values of enumMONDAY("Day 1"),TUESDAY("Day 2"),WEDNESDAY("Day 3"),THRUSDAY("Day 4"),FRIDAY("Day 5"),SATURDAY("Day 6"),SUNDAY("Day 7");privatefinalString description;// private construtor to set default valueWEEKDAY(String description){this.description = description;}// getter method to get the descriptionpublicString getDescription (){returnthis.description;}}publicclassTester{publicstaticvoidmain(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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *