• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

curiocity regarding Enum ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have 3 question's ,
1. why Enum is the best way to create singleton ?
2. does this statement create creates an instance ?
3. In my Enum I have only 2 method from where does I get valueOF(...), values() for free ?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:
1. why Enum is the best way to create singleton ?

desiging a singleton class is not an easy task. you need to consider cloning, servialization, threading etc.. all the effort is done in the Enum.

naved momin wrote:
2. does this statement create creates an instance ?


No, it just assign an existing object to e.

naved momin wrote:
3. In my Enum I have only 2 method from where does I get valueOF(...), values() for free ?


java.lang.Enum
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:
2. does this statement create creates an instance ?

No, it just assign an existing object to e.


"existing object" means object of type Elvis ?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:

Seetharaman Venkatasamy wrote:
2. does this statement create creates an instance ?

No, it just assign an existing object to e.


"existing object" means object of type Elvis ?



Correct.

When an enum class is loaded, one instance for each enum constant is created. That's all that will ever be created for that enum class in that classloader.


When the Color class loads, three Color instances are created--one each for RED, GREEN, and BLUE. No other instances of Color will ever be created (unless we load that class in a different ClassLoader).
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

naved momin wrote:

Seetharaman Venkatasamy wrote:
2. does this statement create creates an instance ?

No, it just assign an existing object to e.


"existing object" means object of type Elvis ?



Correct.

When an enum class is loaded, one instance for each enum constant is created. That's all that will ever be created for that enum class in that classloader.


When the Color class loads, three Color instances are created--one each for RED, GREEN, and BLUE. No other instances of Color will ever be created (unless we load that class in a different ClassLoader).

thanks jeff
reply
    Bookmark Topic Watch Topic
  • New Topic