What Is an Enum Type

This section describes 'enum' types, which are defined by 'enum' declaration statements. An 'enum' type is actually a special kind of class type.

What Is an Enum Type? An enum type is a special kind of class type that has a set of pre-defined instances representing a set of static values called enum constants.

An enum type is defined by the "enum" declaration statement. A simplest enum Type can be defined with 2 components:

Here is an example of "enum" declaration statement:

/* Color.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
enum Color {
   // enum constants declaration and initialization

The above "enum" declaration statement actually:

We can verify the above with this test program:

/* ColorTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
class ColorTest {
   public static void main(String[] arg) {

      System.out.println("Converting enum constant to string:");
      System.out.println("   Color.RED = "+Color.RED);
      System.out.println("   Color.GREEN = "+Color.GREEN);
      System.out.println("   Color.BLUE = "+Color.BLUE);

      System.out.println("Getting enum constant class name:");
      String clsName = Color.RED.getClass().getName();
      System.out.println("   Color.RED.getClass().getName() = "+clsName);

If you compile ColorTest.java with Color.java and run ColorTest.class, you should get:

herong> javac Color.java

herong> javac ColorTest.java

herong> java ColorTest
Converting enum constant to string:
   Color.RED = RED
   Color.GREEN = GREEN
   Color.BLUE = BLUE
Getting enum constant class name:
   Color.RED.getClass().getName() = Color

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

Enum Types and Enum Constants

What Is an Enum Type

 Use 'class' to Define Enumeration

 Instance Variables for Enum Constants

 java.lang.Enum Super Type

 Enum Constant Inherited Methods

 Enum Constant Implicit Methods

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials


 Full Version in PDF/EPUB