File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum constructors" Watch "enum constructors" New topic
Author

enum constructors

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
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?


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Panumat Wiwatmaikul
Greenhorn

Joined: Jan 25, 2009
Posts: 10
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?


SCJP 6 87%
("; ) LuCisFero
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Panumat Wiwatmaikul
Greenhorn

Joined: Jan 25, 2009
Posts: 10
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."
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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
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
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
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
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
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
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://aspose.com/file-tools
 
subject: enum constructors
 
Similar Threads
Please suggest me why im getting sysouts of super class
Threas - Object Locks Question
Constructors
Hash code
Using THIS