March 2019
Intermediate to advanced
242 pages
6h 21m
English
Prior to the introduction of enums, developers often used public, static, and final variables to define constants. The following is an example:
class Size {
public final static int SMALL = 1;
public final static int MEDIUM = 2;
public final static int LARGE = 3;
}
The major drawback of using public, static, final, and int variables is type safety; any int value could be assigned to a variable of the type int, instead of the Size.SMALL, Size.MEDIUM, or Size.LARGE constants.
Java 5 introduced enums, an addition to the language construct, to enable developers to define an enumeration of constants. Here's a quick example:
enum Size {SMALL, MEDIUM, LARGE} class SmallTShirt { Size size = Size.SMALL; ...Read now
Unlock full access