Enum Constants

8.9.1 Enum Constants

The body of an enum type may contain enum constants. An enum constant defines an instance of the enum type.

CLASSES Enum Constants 8.9.1

EnumConstants: EnumConstant EnumConstants , EnumConstant

EnumConstant: Annotations opt Identifier Arguments opt ClassBody opt

Arguments: ( ArgumentList opt )

EnumBodyDeclarations: ; ClassBodyDeclarations opt

An enum constant may optionally be preceded by annotation modifiers. If an annotation a (§9.7) on an enum constant corresponds to an annotation type T (§9.6), and T has a (meta-)annotation m that corresponds to java.lang.annotation.Target , then m must have an element whose value is java.lang.annotation.ElementType.FIELD , or a compile-time error occurs.

The Identifier in a EnumConstant may be used in a name to refer to the enum constant.

The scope and shadowing of an enum constant is specified in §6.3 and §6.4. An enum constant may be followed by arguments, which are passed to the

constructor of the enum type when the constant is created during class initialization as described later in this section. The constructor to be invoked is chosen using the normal overloading rules (§15.12.2). If the arguments are omitted, an empty argument list is assumed.

The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type. The class body is governed by the usual rules of anonymous classes; in particular it cannot contain any constructors.

Instance methods declared in these class bodies may be invoked outside the enclosing enum type only if they override accessible methods in the enclosing enum type.

It is a compile-time error for the class body of an enum constant to declare an abstract method.

Because there is only one instance of each enum constant, it is permissible to use the == operator in place of the equals method when comparing two object references if it is known that at least one of them refers to an enum constant.

8.9.1 Enum Constants CLASSES

The equals method in Enum is a final method that merely invokes super.equals on its argument and returns the result, thus performing an identity comparison.

Example 8.9.1-1. Iterating Over Enum Constants With An Enhanced for Loop

public class Test {

enum Season { WINTER, SPRING, SUMMER, FALL }

public static void main(String[] args) {

for (Season s : Season.values()) System.out.println(s);

This program produces the output: WINTER

SPRING SUMMER FALL

Example 8.9.1-2. Use Of java.util.EnumSet For Subranges

import java.util.EnumSet;

public class Test { enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }

public static void main(String[] args) {

System.out.print("Weekdays: "); for (Day d : EnumSet.range(Day.MONDAY, Day.FRIDAY))

System.out.print(d + " ");

This program produces the output: Weekdays: MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY

java.util.EnumSet contains a rich family of static factories, so this technique can be generalized to work with non-contiguous subsets as well as subranges. At first glance, it might appear wasteful to generate a java.util.EnumSet object for a single iteration, but they are so cheap that this is the recommended idiom for iteration over a subrange. Internally, a java.util.EnumSet is represented with a single long assuming the enum type has 64 or fewer elements.

CLASSES Enum Body Declarations 8.9.2