Why can a enum have a package-private constructor?

Tobias picture Tobias · Oct 13, 2011 · Viewed 16.8k times · Source

Since an enum constructor can only be invoked by its constants, why is it then allowed to be package-private?

Answer

ColinD picture ColinD · Oct 13, 2011

The constructor actually isn't package-private... it's implicitly private the way interface methods are implicitly public even if you don't add the keyword.

The relevant section of the JLS (§8.8.3) states:

If no access modifier is specified for the constructor of a normal class, the constructor has default access.

If no access modifier is specified for the constructor of an enum type, the constructor is private.

It is a compile-time error if the constructor of an enum type (§8.9) is declared public or protected.