Let Me Count the Ways: Using the enum
Most applications need to keep track of a finite set of values—constants that represent a set of choices or states in the application. One common Java programming practice is to use static int variables to represent these values. Programs then make decisions by comparing the values of other variables against them. Although the core Java API itself uses this practice, it can lead to serious problems! The following example class returns information about fruits for an imaginary menu-planning system. It shows some of the problems in using int variables to represent enumerated data:
public class FruitConstants ...
Get Wicked Cool Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.