Sharper typing of enum constants

One of the two major goals of enhanced enums is to perform sharper type-checking for enums. At present, the type of all enum constants is the enum in which they are defined. Referring to our example enum, Size, this essentially means that the type of all of the enum constants (SMALL, MEDIUM, and LARGE) is Size, which is incorrect (as depicted in the following figure):

Although enum constants are allowed to define a constant specific class body, which includes variables and methods, the constant type is not sharp enough to allow for access to enum constant-specific values. Even in the case of generic enums, ...

Get Java 11 and 12 - New Features 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.