Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : E words : enum.
Don't confuse enum with the Enumeration interface, an almost obsolete way of iterating over Collections, replaced by Iterator.
Java 1.5 enums are references to a fixed set of Objects than represent the various possible choices. Enums handle single choices, not combinations of choices. For combinations, you need EnumSet.
Enums and the enum constants are just classes. Normally they should be capitalised. However, since the constant name is the same as what is displayed externally, I gather the capitalisation rules are relaxed. You would name your constants with upper or lower case names as appropriate for the application.
Needless to say, these enums are type safe. If you try to store a code for a dog Breed into a Locomotive type enum variable, the compiler will refuse.
Enum constants can store additional data, and can have custom methods.
enum Breed { // the enum constants Dalmatian ( "spotted" ), Labrador ( "black" ), Dachshund( "brown" ); // constructor Breed ( String colour ) { this.colour = colour; } private String colour; // additional method of every Breed object public String getColour() { return colour; } }
Enum constants can even each have their own little anonymous class.
EnumSets allow you a Pascalian ability to talk about any combination of enums. Sorry, I don't know how it works. The documentation reads like Greek.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 2476. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/enum.html | J:\mindprod\jgloss\enum.html | |||