GeeCON Prague 2014*
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


JavaRanch » Java Forums » Java » Java in General
Bookmark "Enums" Watch "Enums" New topic
Author

Enums

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.


Cheers!
RSR
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
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.
 
GeeCON Prague 2014
 
subject: Enums