File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Enum access specifier

 
sharma ishu
Ranch Hand
Posts: 70
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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: 4549
8
Java Netbeans IDE Scala
  • 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
  • 0
  • 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: 4549
8
Java Netbeans IDE Scala
  • 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
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic