March 2019
Intermediate to advanced
242 pages
6h 21m
English
Imagine limiting the sizes of a shirt to some predefined sizes (such as Small, Medium, and Large). The following code shows how you can do that with an enum (Size):
enum Size {SMALL, MEDIUM, LARGE}
The following code shows how you can use the Size enum in a class, Shirt, to restrict its sizes to constants defined in the Size enum:
class Shirt {
Size size; // instance variable of type Size
Color color;
Shirt(Size size, Color color) { // Size object with Shirt // instantiation
this.size = size;
this.color = color;
}
}
The instance variable of the Size type in the ...
Read now
Unlock full access