From the course: Java 11+ Essential Training

Unlock the full course today

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

Declare and use Enum types

Declare and use Enum types - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Declare and use Enum types

- [Instructor] I've previously described using constants as a way to make sure that you type values correctly. The compiler enforces the names of the constants so that you're not using literal strings. There's another more advanced approach called an Enumerator or just an enum. It's a special kind of Java class that contains possible values. I'm going to change the way the size of each ClothingItem is stored. Instead of being a simple string, it'll be an instance of an enum. First, I need to create that new class in my model package. I'll name it ClothingSize and I'll set it as an Enumerator, enum. An enum is marked this way with enum, instead of class. Now, I'll set possible values. The simplest way of doing this is simply to type the values in a comma-delimited list and I'll put an S, M, and L for small, medium, and large. Now I'll go back to my ClothingItem class and I'll change the type of my Size…

Contents