aspose 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
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: 13875
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
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"?
 
Similar Threads
Doubt on Enums
Enum Constructor - Exception and Accessibility
access control modifier for enum
enum constructor help
where we use enums