March 2019
Intermediate to advanced
242 pages
6h 21m
English
You can add states and behaviors to enum constants by defining instance variables and methods in an enum. All of these are accessible by the enum constants. Let's modify the Size enum defined in the previous section, by adding a state and behavior to it. Each enum constant can define a constant, specific class body, define a new state and behavior, or override the default behavior of the enum methods in which it is defined. The following is an example of this:
enum Size {
SMALL(36, 19),
MEDIUM(32, 20) { // Constant specific class body
int number = 10; // variable specific to //MEDIUM
int getSize() { // method specific to //MEDIUM return length + width; } }, LARGE(34, 22) { @Override public String ...Read now
Unlock full access