From the course: Java SE 17 Developer (1Z0-829) Cert Prep

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Enum values and initialization

Enum values and initialization - Java Tutorial

From the course: Java SE 17 Developer (1Z0-829) Cert Prep

Enum values and initialization

- Now, let's investigate enum types and their initialization. Enumerated types provide named instances which are defined at compilation time, for example, MONDAY, TUESDAY, and so forth. Importantly, values cannot be added or removed dynamically from the set of known values. If the value set changes, you have to recompile the program. The instances are type-safe. That's an important difference from simply using numbers to indicate these values. And Java provides for these using a special class that will be declared as an enum. And that will look something like this. We say public enum instead of public class, and then some description, which is the class name in fact, indicating what it is that we are enumerating. Then we provide a list of the values, and these will actually be object references, objects of the Suit type in this case. And we put a semicolon on the end. An observation is that if the only thing we're providing in this enum class is the list of values, the semicolon is…

Contents