This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv 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 REST with Spring (video course) this week in the Spring 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.
It is sorta covered in the JavaRanch Style Guide.
subject: Enums
It's not a secret anymore!