This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Understanding enum 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 "Understanding enum" Watch "Understanding enum" New topic
Author

Understanding enum

Ravi Shankar Kumar
Ranch Hand

Joined: Aug 08, 2008
Posts: 30
    
    1

Hi

can anybody tell me how enum is advantageous if i am using inside class instead of static variable keeping out of this.....


Ravi Shankar Kumar
SCJP 1.5, (Following-OCWCD)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3983
    
  18

Can you please rephrase your question? I'm not sure what you meant.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Sebanti Sanyal
Ranch Hand

Joined: Nov 07, 2011
Posts: 58

If you strictly want a variable to assume a value within a specific set of constants, you should be better off with an enum.


In the above example, 'Color c' can take only four values(e.g. Color.RED, Color.YELLOW, Color.BLUE or Color.GREEN), whereas, in case of 'int color', nothing stops me from assigning any value outside the four static final int constants.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3983
    
  18

You've already identified the most important advantage yourself.

It doesn't make sense for a Color to be 1 or 2 or 5. Colors should be RED or GREEN, etc. and there should be nothing else - like an integer value - that is of importance.

Enums provide this important distinction.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding enum
 
It's not a secret anymore!