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...
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...
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."
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
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?
All code in my posts, unless a source is explicitly mentioned, is my own.
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
posted
0
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.
SCJP 6
Ruben Soto
Ranch Hand
Joined: Dec 16, 2008
Posts: 1032
posted
0
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.
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
Joined: Dec 16, 2008
Posts: 1032
posted
0
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?
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
Joined: Dec 16, 2008
Posts: 1032
posted
0
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
Joined: Oct 16, 2008
Posts: 952
posted
0
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
Joined: Dec 16, 2008
Posts: 1032
posted
0
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
Joined: Oct 16, 2008
Posts: 952
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.