File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion 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: 4490
    
    8

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: 61764
    
  67

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