Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Enum

Java Enum

A Java enum is a special class, generally used to represent a group of constants, such as the four seasons of a year, the twelve months of a year, the seven days of a week, or the four directions (east, south, west, north).

Java enum classes are defined using the enum keyword, with the constants separated by commas (,).

For example, define an enum class for colors.

enum Color 
{ 
    RED, GREEN, BLUE; 
} 

The Color enum above has color constants RED, GREEN, BLUE, representing red, green, and blue respectively.

Usage example:

Example

enum

Color

{

    RED, GREEN, BLUE

;

}

 

public

class

Test

{

   

// Execute and output the result

   

public

static

void

main

(

String

[

]

args

)

   

{

       

Color

c1

=

Color

.

RED

;

       

System

.

out

.

println

(

c1

)

;

   

}

}

The output of the code above is:

RED

Using enums in inner classes

Enum classes can also be declared within inner classes:

Example

public

class

Test

{

   

enum

Color

   

{

        RED, GREEN, BLUE

;

   

}

 

   

// Execute and output the result

   

public

static

void

main

(

String

[

]

args

)

   

{

       

Color

c1

=

Color

.

RED

;

       

System

.

out

.

println

(

c1

)

;

   

}

}

The output of the code above is:

RED

Each enum is implemented internally via Class, and all enum values are public static final.

The Color enum above is implemented as an inner class:

class Color
{
     public static final Color RED = new Color();
     public static final Color BLUE = new Color();
     public static final Color GREEN = new Color();
}

Iterating Over Enum Elements

You can use a for statement to iterate over enum elements:

Example

enum

Color

{

    RED, GREEN, BLUE

;

}

public

class

MyClass

{

 

public

static

void

main

(

String

[

]

args

)

{

   

for

(

Color

myVar

:

Color

.

values

(

)

)

{

     

System

.

out

.

println

(

myVar

)

;

   

}

 

}

}

The output of the code above is:

RED
GREEN
BLUE

Using enum classes in a switch statement

Enum classes are often used in switch statements:

Example

enum

Color

{

    RED, GREEN, BLUE

;

}

public

class

MyClass

{

 

public

static

void

main

(

String

[

]

args

)

{

   

Color

myVar

=

Color

.

BLUE

;

   

switch

(

myVar

)

{

     

case

RED

:

       

System

.

out

.

println

(

"Red"

)

;

       

break

;

     

case

GREEN

:

         

System

.

out

.

println

(

"Green"

)

;

       

break

;

     

case

BLUE

:

       

System

.

out

.

println

(

"Blue"

)

;

       

break

;

   

}

 

}

}

The output of the code above is:

蓝色

The values(), ordinal(), and valueOf() Methods

An enum class defined with enum implicitly inherits the java.lang.Enum class and implements the java.lang.Serializable and java.lang.Comparable interfaces.

The values(), ordinal(), and valueOf() methods are located in the java.lang.Enum class:

Example

enum

Color

{

    RED, GREEN, BLUE

;

}

 

public

class

Test

{

   

public

static

void

main

(

String

[

]

args

)

   

{

       

// Call values()

       

Color

[

]

arr

=

Color

.

values

(

)

;

 

       

// Iterate over the enum

       

for

(

Color

col

:

arr

)

       

{

           

// Check the index

           

System

.

out

.

println

(

col

" at index "

col.

ordinal

(

)

)

;

       

}

 

       

// Use valueOf() to return the enum constant; a non-existent one throws IllegalArgumentException

       

System

.

out

.

println

(

Color

.

valueOf

(

"RED"

)

)

;

       

// System.out.println(Color.valueOf("WHITE"));

   

}

}

The output of the code above is:

RED at index 0
GREEN at index 1
BLUE at index 2
RED

Enum Class Members

Like ordinary classes, enums can have their own variables, methods, and constructors. The constructor can only use the private access modifier, so it cannot be called externally.

An enum can contain both concrete and abstract methods. If the enum class has an abstract method, each instance of the enum class must implement it.

Example

enum

Color

{

    RED, GREEN, BLUE

;

 

   

// Constructor

   

private

Color

(

)

   

{

       

System

.

out

.

println

(

"Constructor called for : "

this

.

toString

(

)

)

;

   

}

 

   

public

void

colorInfo

(

)

   

{

       

System

.

out

.

println

(

"Universal Color"

)

;

   

}

}

 

public

class

Test

{

   

   

// Output

   

public

static

void

main

(

String

[

]

args

)

   

{

       

Color

c1

=

Color

.

RED

;

       

System

.

out

.

println

(

c1

)

;

        c1.

colorInfo

(

)

;

   

}

}

The output of the code above is:


Constructor called for : RED
Constructor called for : GREEN
Constructor called for : BLUE
RED
Universal Color