This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Enum access specifier

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what does this line means.

"The constructor for an enum type must be package-private or private access."
 
Guillaume Jourdan
Ranch Hand
Posts: 47
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sentence means that you can't implement an enum outside the enum class definition. By default, an enum have a private default constructor, but you can define your own constructor.

Example :

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseAMeaningfulSubjectLine henceforth. I have modified it for you this time.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to add to what Guillaume said - try adding a constructor to the enum that has a public or protected modifier. You should get a compiler error.

To be honest, I'm not sure why they allow a package-accessible constructors as well as private, but it may be just so that you can get away with not giving a modifier.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:And to add to what Guillaume said - try adding a constructor to the enum that has a public or protected modifier. You should get a compiler error.

To be honest, I'm not sure why they allow a package-accessible constructors as well as private, but it may be just so that you can get away with not giving a modifier.


But we cannot say like: new EnumType();
So, what difference does it make that what access modifier is used for the constructor.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sharma ishu wrote:But we cannot say like: new EnumType();
So, what difference does it make that what access modifier is used for the constructor.

Look at it the other way: since you can't go new EnumType(), what possible reason could there be to have a public or protected constructor? There's a general tendency in the design of Java to disallow things that don't make sense.
 
RajaShekhar Gundeti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems enums are used to declare a fixed set of meaningful constants. so, it doesn't allow creations of enums dynamically at runtime by restricting using access specifier. enums are static and compile.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic