O'Reilly logo

Java: A Beginner’s Tutorial, 4th Edition (Updated for Java SE 8) by Budi Kurniawan

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

Chapter 12

Enums

In Chapter 2, “Language Fundamentals” you learned that you sometimes use static final fields as enumerated values. Java 5 added a new type, enum, for enumerating values. You will learn how to create and use enums in this chapter.

Overview

You use an enum to create a set of valid values for a field or a method. For example, in a typical application, the only possible values for the customer type are Individual or Organization. For a State field, valid values may be all the states in the US plus Canadian provinces, and probably some others. With an enum, you can easily restrict your program to take only one of the valid values.

The enum type can stand alone or can be part of a class. You make it stand alone if it needs to be referenced ...

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