This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Enums" Watch "Enums" New topic


rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
Hi All,

Jdk1.5 has enums right? I see enums as a way of reducing developer errors. Is that the main purpose of enums? Can someone give a simple example that explains the use of enums and what problems would we have faced without them.

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2068
Here's one example.

public enum SEX { MALE, FEMALE }

If you use this enum, your data will for sure contain only one of those 2 possible values.

If you use String instead of enum, like

String SEX;

you will have possibility of mistakenly putting 'SHE-MALE' on it (not that there is no such this in reality), but your system is expecting only 'MALE' or 'FEMALE'. Add also the fact that in comparing string, you must trim() and compare ignoring case.
I agree. Here's the link:
subject: Enums
jQuery in Action, 3rd edition