Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

enum constructors

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can someone confirm they can only have default access or private access?
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 67637
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
default
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic