File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Why can an enum constructor be Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why can an enum constructor be "package-private"?" Watch "Why can an enum constructor be "package-private"?" New topic
Author

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

Hiram Nascimento
Greenhorn

Joined: Jul 09, 2009
Posts: 13
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

Joined: Aug 16, 2005
Posts: 14589
    
  24

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Hiram Nascimento
Greenhorn

Joined: Jul 09, 2009
Posts: 13
Thanks! Your tip of querying the JLS will help me a lot on my SCJP preparation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can an enum constructor be "package-private"?