ByteBrawl - Enum Class - Classes And Objects - Java Basics

Classes And Objects - Enum Class

Enum, short for enumeration, is a special type of class that represents a fixed set of constants. They are useful when you have a variable that can only take on a limited number of values. For example, we can define an enum for DogBreed:

One way we can use this is printing its value:

Connecting enums to the Dog class

We can also use enums to reference specific instances of a class. Since an enum is a class, we can add fields and constructors to it (and methods too). Let's add a dog field to DogBreed with a constructor for it:

Now, each enum constant represents a specific Dog instance. Let's try accessing these instances: