Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enum constructor

 
prakash Venugopal
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an enum constructor can only be used within the enum itself to construct the elements, it cannot be public or protected.
 
Ted North
Ranch Hand
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If By default access of enum is private, then the enum is singleton is it?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
prakash Venugopal
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prakash Venugopal wrote: then the enum is singleton is it?

question for you: what is the definition of singleton?
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
1
Java Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic