• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enum constructors

 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can someone confirm they can only have default access or private access?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
default
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:default


so "package-private" means default?

 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic