Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why can an enum constructor be "package-private"?

 
Hiram Nascimento
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that an enum constructor can't be invoked anywhere, why can an enum constructor be "package-private"?

Even if the constructor is private, the enum's constructor can't be invoked inside of it.

Thank you!
Hiram
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, your code doesn't compile because there's a semi-colon lacking at the end of line 3.

You can't instantiate enum types, so it's not really necessary to make the constructor explicitly private. However, the compiler does allow you to explicitly make it private, even though that doesn't really mean anything in this context. I don't know why the Java language inventors choose to do it this way.

Section 8.9 of The Java Language Specification describes exactly how enums work in Java.
 
Hiram Nascimento
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! Your tip of querying the JLS will help me a lot on my SCJP preparation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic