Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Enum constructor can't have protected or public access modifier

Tags:

java

enums

Enum constructors must be either private or package default, and protected or public access modifier is not allowed. Why so

like image 776
Pramod Kumar Avatar asked Jan 16 '23 03:01

Pramod Kumar


1 Answers

Because an enum, by definition, has a fixed set of instances which are declared and constructed in the enum itself. Using the constructor from outside of the enum class itself thus doesn't make sense.

And AFAIK, an enum constructor is always, explicitely or implicitely, private.

like image 197
JB Nizet Avatar answered Jan 30 '23 03:01

JB Nizet