O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting ready

Let's have a quick look at a couple of examples. Here is the simplest possible enum type:

public enum RoadCondition {  DRY, WET, SNOW}

Say we run the following loop:

for(RoadCondition v: RoadCondition.values()){  System.out.println(v);}

The results of this will be as follows:

The enum type implicitly extends java.util.Enum (so you cannot extend your custom type RoadCondition, for example) and automatically acquires its methods. In the preceding code, we have already seen one (the most useful) method, values(), that returns an array of the enum elements.

Another useful enum method is valueOf(String) that returns the constant of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required