Use 'class' to Define Enumeration

This section provides a tutorial on how to define a set of enumeratin constants use a 'class' declaration statement.

If you don't like to use 'enum' declaration statements, you can use 'class' declaration statement to define an enumeration. Here is an example of "class" declaration statement defining as set of enumeration constants:

/* Season.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class Season {
   public static Season WINTER = new Season("WINTER");
   public static Season SPRING = new Season("SPRING");
   public static Season SUMMER = new Season("SUMMER");
   public static Season FALL = new Season("FALL");
   private String label = null;
   Season(String label) { this.label = label; }
   public String toString() { return label; }
}

We can see how those enumeration constants behave with this test program:

/* SeasonTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class SeasonTest {
   public static void main(String[] arg) {
      System.out.println("Converting enum constant to string:");
      System.out.println("   Season.WINTER = "+Season.WINTER);
      System.out.println("   Season.SPRING = "+Season.SPRING);
      System.out.println("   Season.SUMMER = "+Season.SUMMER);
      System.out.println("   Season.FALL = "+Season.FALL);

      System.out.println("Getting enum constant class name:");
      String clsName = Season.WINTER.getClass().getName();
      System.out.println("   Season.WINTER.getClass().getName() = "
         +clsName);
   }
}
ß

If you compile SeasonTest.java with Season.java and run SeasonTest.class, you should get:

herong> javac Season.java

herong> javac SeasonTest.java

herong> javac SeasonTest
Converting enum constant to string:
   Season.WINTER = WINTER
   Season.SPRING = SPRING
   Season.SUMMER = SUMMER
   Season.FALL = FALL
Getting enum constant class name:
   Season.WINTER.getClass().getName() = Season

Cool. We have defined a set of enumeration constants with a "class" declaration statement.

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

 References

 Full Version in PDF/EPUB