aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enum constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Enum constructor" Watch "Enum constructor" New topic
Author

Enum constructor

prakash Venugopal
Greenhorn

Joined: Dec 28, 2011
Posts: 8


while i compiling the program it gives public modifiers not allowed here. The same is happen for protected modifier.
But if i put private or default it is working fine.
I declared the enum outside the class.
why?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61652
    
  67

As an enum constructor can only be used within the enum itself to construct the elements, it cannot be public or protected.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

prakash Venugopal wrote:

while i compiling the program it gives public modifiers not allowed here. The same is happen for protected modifier.
But if i put private or default it is working fine.
I declared the enum outside the class.
why?



enums can be a class of their own or inside a class. These are interesting parts of the Java language. Like a class, an enum as its own class can only have default or public access modifier.
As you noted an enum's constructor can only be default or private. This is to prevent lots of modifications to the enum I think, sort of life preventing 'race-conditions' with multiple Threads accessing and changing an objects state. Either that or this is just the rules of Java.

A pro answers this question here:
http://www.coderanch.com/t/449434/java-programmer-SCJP/certification/enum-constructor-private

Read Pete Stein's posts...
prakash Venugopal
Greenhorn

Joined: Dec 28, 2011
Posts: 8
If By default access of enum is private, then the enum is singleton is it?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

prakash Venugopal wrote:If By default access of enum is private, then the enum is singleton is it?


Why this doubt arose in your mind that it is really singleton or not ?, first read what is meaning of singleton and think in context for enum that it shall be singleton or not.


Tell the difficulties that i am difficult.
prakash Venugopal
Greenhorn

Joined: Dec 28, 2011
Posts: 8
If the constructor is private then the instance of the enum should be one. thats why i told enum may be a singleton. is it right or wrong? please clear my doubt...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

prakash Venugopal wrote: then the enum is singleton is it?

question for you: what is the definition of singleton?
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

prakash Venugopal wrote:If the constructor is private then the instance of the enum should be one. thats why i told enum may be a singleton. is it right or wrong? please clear my doubt...


Hope this will help you http://en.wikipedia.org/wiki/Singleton_pattern
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

prakash Venugopal wrote:If the constructor is private then the instance of the enum should be one. thats why i told enum may be a singleton. is it right or wrong? please clear my doubt...


Singleton design pattern is beyond the scope of the Java Programmer exam, but I think its basically a singleton. Normally a singleton class, that is not an enum, has additional things like static members to keep the instantiations down to 1. Hence the name Singleton.

Please find these examples of Singletons: http://www.javapractices.com/topic/TopicAction.do?Id=46

Looks like you are totally correct that an enum can be used as quiet the Singleton design pattern. Learning everyday here in Ted North land.

I wonder why a default access modifier is acceptable though? Wouldn't his mean that any class in the package could instantiate the enum?

Respectfully,

TN - byte code for breakfast

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ted North wrote:
I wonder why a default access modifier is acceptable though? Wouldn't his mean that any class in the package could instantiate the enum?

if you are lazy to type private for enum constructor, compiler do that for you. ;)
Ted North
Ranch Hand

Joined: Jan 02, 2012
Posts: 193
    
    1

Seetharaman Venkatasamy wrote:
Ted North wrote:
I wonder why a default access modifier is acceptable though? Wouldn't his mean that any class in the package could instantiate the enum?

if you are lazy to type private for enum constructor, compiler do that for you. ;)


Alright, this makes logical sense.

Thank-you for the friendly reply Seetharaman. : )

Respectfully,

TN
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ted North wrote:
Thank-you for the friendly reply Seetharaman. : )

you are welcome, of course.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enum constructor