|
Helping ordinary people create extraordinary websites! |
Getting Started with Enumerated TypesBy Brett McLaughlin2005-04-22
Defining an Enum Listing 2 uses an enumeration to provide similar functionality to Listing 1: Listing 2. Simple enumerated type package com.oreilly.tiger.ch03; Here I've used the new keyword enum, given the enum a name, and specified the allowed values. Grade then becomes an enumerated type, which you can use in a manner shown in Listing 3: Listing 3. Using an enumerated type package com.oreilly.tiger.ch03;By creating a new enumeration (grade) of the previously defined type, you can then use it like any other member variable. Of course, the enumeration can be assigned only one of the enumerated values (for example, A, C, or INCOMPLETE). Also, notice how there is no error checking code or boundary considerations in assignGrade(). Tutorial Pages: » Represent Constants in a Typesafe Manner Using Java 5.0 » Defining an Enum » Working with Enumerated Values » Enums and Collections » Going Further » Use Them, But Don't Abuse Them » Resources First published by IBM developerWorks |
|