This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is the usage on enum types? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the usage on enum types?" Watch "What is the usage on enum types?" New topic
Author

What is the usage on enum types?

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
What is the usage on enum types and where they are useful?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

Anywhere you to abstract a finite set of values. A classic example is card suits:

The primary advantages are code readability and type-safety.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

If you know all the possible states of what you are describing in your code when you compile it then you should use enumerated types. Simples.


You're on a gravy train with biscuit wheels Roy.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11168
    
  16

A (smallish) finite set of things is always a good candidate. As Bear's example illustrates, it's much easier to read and understand code that says

if (card.getSuit().equals(HEARTS))

than code that says

if (card.getSuit() == 2)


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
 
subject: What is the usage on enum types?
 
Similar Threads
Storing enums in database
comparing two enum types
Enum
enum
is there a quicker way to read in input besides bufferedreader?