This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum constructors" Watch "enum constructors" New topic
Author

enum constructors

mark juszczec
Ranch Hand

Joined: Aug 20, 2011
Posts: 35

Can someone confirm they can only have default access or private access?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

The constructor for an enum type must be package-private or private access.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4246
    
    7

I think you can declare an enum constructor public, but it doesn't mean you can use it from outside the class - there's a special mechanism for enums that prevents it.

Edit: OK, apparently not . As always, there's only one way to be sure...just tried it out, and it won't compile with a public constructor: "Modifier public not allowed here".

Since you can't create your own enums, a private contructor makes perfect sense. I must admit, I'm not sure why they bother allowing a package-access constructor, but I'm probably missing something.
mark juszczec
Ranch Hand

Joined: Aug 20, 2011
Posts: 35
Dennis Deems wrote:http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

The constructor for an enum type must be package-private or private access.


This got me started fiddling with enum constructor defs.

What the heck is "package-private"?

AFAIK, there's public, protected, default and private.

Is "package-private" a new way to describe on of those?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

default


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
mark juszczec
Ranch Hand

Joined: Aug 20, 2011
Posts: 35
Matthew Brown wrote:I think you can declare an enum constructor public, but it doesn't mean you can use it from outside the class - there's a special mechanism for enums that prevents it.

Edit: OK, apparently not . As always, there's only one way to be sure...just tried it out, and it won't compile with a public constructor: "Modifier public not allowed here".

Since you can't create your own enums, a private contructor makes perfect sense. I must admit, I'm not sure why they bother allowing a package-access constructor, but I'm probably missing something.


I tried:


and got:

bash-3.2$ javac EnumTest.java
EnumTest.java:8: modifier public not allowed here
public MyEnum(int y){
^
1 error
mark juszczec
Ranch Hand

Joined: Aug 20, 2011
Posts: 35
Bear Bibeault wrote:default


so "package-private" means default?

dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
mark juszczec wrote:
Bear Bibeault wrote:default


so "package-private" means default?



Yes, but you won't find people using it much. Generally they'll just say "package" or "default". IMO "package-private" is unnecessarily confusing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum constructors
 
Similar Threads
Doubt in Security contraints
Help on SimpleTag
protected access of a class
Abstract methods and access modifier
Please help me need urgent help on JPA connection