aspose file tools
The moose likes Java in General and the fly likes Java Classes VS enums Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Java Classes VS enums" Watch "Java Classes VS enums" New topic
Author

Java Classes VS enums

K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
Hi All,

Enums in Java are now defined as special case/implementation for type CLASS. 1.5 Onwards Java empowered enums with some real good features to make it more meaningful and flexible .. but doing so one thing was seen that it enum became more or less CLASS construct... All things which are possible in Enum are more or less found to be easility migratable to Class Contruct... this has coined couple of doubts in my mind...
1. what is distinguishing characteristic of Enum fron ordinary classes. I mean is there anything which can be done ONLY in enum but NOT in class OR VICE-VERSA.
2. Which one should be used when? How one should take a call that I should leverage Enum contruct than the general CLASS construct
Thanks in advance.

“The difference between 'involvement' and 'commitment' is like an eggs-and-ham breakfast: the chicken was 'involved' - the pig was 'committed'.”
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

K Abhijit wrote:1. what is distinguishing characteristic of Enum fron ordinary classes. I mean is there anything which can be done ONLY in enum but NOT in class OR VICE-VERSA.

Being able to use them in a switch. That's it. All of the rest can be achieved by using a regular class with private constructor and public static final fields; properly implementing the readResolve method will guarantee your instances will not be duplicated when serializing.

2. Which one should be used when? How one should take a call that I should leverage Enum contruct than the general CLASS construct

Enums could (should?) be used when:
- you do not need to extend a class (enums cannot extend a class; implementing interfaces is not a problem)
- your enum does not need to be extended (enums are final)
- you have a fixed set of values that does not need to change during runtime
(there are probably a few more items but I can't think of any right now)


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Rob Prime wrote: . . . (there are probably a few more items but I can't think of any right now)
You want a true singleton?
K Abhijit
Ranch Hand

Joined: Mar 03, 2008
Posts: 88
Thanks a lot Rob and Campbell .....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
You're welcome
 
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.
 
subject: Java Classes VS enums
 
Similar Threads
Importing enums
yet not clear about what is the actual use of enumerated types ?
Where To Package Enums
Can enum define its serialVersionUID or it has by default?
Tiger enums....