• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

enum constructors

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is from javabeat.net

Will the following program compile?




Answer is no. As Enum constructors cannot be public.

But why? cant it be private or have default access modifier?


 
Sheriff
Posts: 9703
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Enum constructor has to be private. This is to stop any construct to create an instance of the enum. That would defeat the purpose of enums. How would you feel if you create an enum of fruits containing Apple, orange and Banana. And someone creates another instance of the enum which is Carrot. This is why enum constructors are private...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:Enum constructor has to be private. This is to stop any construct to create an instance of the enum. That would defeat the purpose of enums. How would you feel if you create an enum of fruits containing Apple, orange and Banana. And someone creates another instance of the enum which is Carrot. This is why enum constructors are private...



As above "Enum constructor has to be private."

I'm still confuse, why default is allowed?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm still confuse, why default is allowed?


8.8.3 Constructor Modifiers
It is a compile-time error if the constructor of an enum type (§8.9) is declared public or protected.
 
Panumat Wiwatmaikul
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:

I'm still confuse, why default is allowed?


8.8.3 Constructor Modifiers
It is a compile-time error if the constructor of an enum type (§8.9) is declared public or protected.



Thank you Christophe.

I'm clear now.

"If no access modifier is specified for the constructor of an enum type, the constructor is private."
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but what is the reason default is allowed? That still lets any classes in the same package as the enum use the enum's constructor, doesn't it?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Yes, but what is the reason default is allowed? That still lets any classes in the same package as the enum use the enum's constructor, doesn't it?



By default enum constructors are private, not default.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that Punit (if you don't provide a constructor for an enum, the default constructor will have private access.) But still default access is allowed in constructors that you code. I'm wondering why.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you look at my post ?
"It is a compile-time error if the constructor of an enum type (§8.9) is declared public or protected.".
Does it say that a constructor must not have the default access ?
1. If no constructor is explicitly provided, a default private constructor will be created.
2. Constructors access may be private or default
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christophe Verré wrote:Did you look at my post ?
"It is a compile-time error if the constructor of an enum type (§8.9) is declared public or protected.".
Does it say that a constructor must not have the default access ?
1. If no constructor is explicitly provided, a default private constructor will be created.
2. Constructors access may be private or default


My question was if there is any reason why it is allowed for enum constructors to have default access. I don't think I ever said in my post that a constructor can't have default access. My question is: Why is it allowed?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't think I ever said in my post that a constructor can't have default access.


Sorry for the misunderstanding Ruben.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries, Christophe. I was wondering if there is any apparent reason why explicitly coded default constructors are allowed, but maybe the SCJP forum is not the right place to ask. Perhaps I'll try my luck in the intermediate forum once I have some time to devote to non-SCJP-specific questions.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:I understand that Punit (if you don't provide a constructor for an enum, the default constructor will have private access.) But still default access is allowed in constructors that you code. I'm wondering why.



If you write constructor with default access, then compiler will add private for you.

See this code



If you see decompiled version of this code, you will get this constructor:

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, that's what's going on! Thank you, Punit. Kind of weird that you are still allowed to code it with default access. I guess they did it that way as a convenience feature maybe.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:Ah, that's what's going on! Thank you, Punit. Kind of weird that you are still allowed to code it with default access. I guess they did it that way as a convenience feature maybe.



Yes you are right, just we have to remember that eventually constructor will have private access to it you write private or not.
 
You got style baby! More than this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic