From the course: Java 8 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Declaring and using enum types

Declaring and using enum types - Java Tutorial

From the course: Java 8 Essential Training

Start my 1-month free trial

Declaring and using enum types

- I've previously described how to standardize values using the static and final keywords. These are java's versions of constants. But, you can also set up enumerations, lists of possible values that you can use for any particular variable. An enumeration in java is called an enum class. You can create an enum class, either in its own separate code file, or as a nested enum if its only going to be used in one java class. I'm going to create an enum class in the enums project, and I'll place it the model folder, the same folder that contains my Olive class. I'll right click on that folder and choose New, Java Class. I'll set the name of the class to OliveName, and I'll set the kind of class to Enum, and click OK. And notice that the big difference is in the keyword that identifies the file. Instead of class, this is an enum. Now, I'll set up the possible values as a simple list, separating the values with commas. Just as with constants, the naming convention is to use all upper case…

Contents