Enumeration
Jan 06 2010
Enumerations
Per Sun's Java documentation, a Java enum "is a type whose fields consist of a fixed set of constants ... you should use enum types any time you need to represent a fixed set of constants."
- Prior to Java 1.5, there were 2 basic ways to define new types: classes and interfaces
- In some cases neither one of the options was sufficient, especially for a finite set of a specific type of data
- Example of some finite sets include grades, planets, compass directions, countries, genders, and ethnicity types
- These types of construct were possible prior to 1.5 but required a lot of work and were prone to issues.
- Enums remove the need for public static final constants.
Defining an Enum
Defining an enum is as simple as creating any other class in Java.
public enum Gender {
MALE, FEMALE, UNKNOWN
};
© 2010 CapTech Ventures, Inc. All Rights Reserved. Legal Notices.