Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
java.lang.Enum Super Type
This section provides a tutorial on how to verify the enum type hierarchy supported by the java.lang.Enum Super Type.
From previous tutorials, we can see that the "enum" declaration statement provides a simpler way to define a set of constants than the "class" declaration statement.
Another benefit of using the "enum" declaration statement is that the resulting enum type is a subtype of java.lang.Enum<E extends Enum*lt;E>>, which implements 3 interfaces: Serializable, Comparable<E>, Constable.
So if you define an enum type E with an "enum" declaration statement, E will automatically become a subtype of Enum<Rank> and Enum<?>. E will also become Serializable, Comparable<E>, and Constable.
Here is a sample program that verifies the type hierarchy of an enum type:
/* EnumTypeHierarchy.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.lang.Enum; import java.lang.Comparable; import java.lang.constant.Constable; import java.io.Serializable; class EnumTypeHierarchy { enum Rank { GOLD, SILVER, BRONZE; } enum Color { RED, GREEN, BLUE; } public static void main(String[] arg) { java.io.PrintStream out = System.out; out.println("Converting enum constant to string:"); out.println(" Rank.GOLD = "+Rank.GOLD); out.println(" Rank.SILVER = "+Rank.SILVER); out.println(" Rank.BRONZE = "+Rank.BRONZE); Rank gold = Rank.GOLD; out.println("Enum constant class:"); out.println(" gold instanceof Rank = "+(gold instanceof Rank)); out.println(" gold class name = "+gold.getClass().getName()); out.println("Reifiable class types - Gold:"); out.println(" is a Rank = "+(gold instanceof Rank)); out.println(" is a Enum<?> = "+(gold instanceof Enum<?>)); out.println(" is a Object = "+(gold instanceof Object)); out.println("Reifiable interface types - Gold:"); out.println(" is Serializable = "+(gold instanceof Serializable)); out.println(" is Comparable<?> = "+(gold instanceof Comparable<?>)); out.println(" is Constable = "+(gold instanceof Constable)); out.println("Non-reifiable types:"); Enum<Rank> enumRank = gold; out.println(" gold is an Enum<Rank> = true"); // Enum<Color> enumColor = gold; out.println(" gold is not an Enum<Color> = false"); Comparable<Rank> comparableRank = gold; out.println(" gold is an Comparable<Rank> = true"); // Comparable<Color> comparableColor = gold; out.println(" gold is an Comparable<Color> = false"); } }
If you compile and run EnumTypeHierarchy.java, you should get:
herong> java EnumTypeHierarchy.java Converting enum constant to string: Rank.GOLD = GOLD Rank.SILVER = SILVER Rank.BRONZE = BRONZE Enum constant class: gold instanceof Rank = true gold class name = EnumTypeHierarchy$Rank Reifiable class types - Gold: is a Rank = true is a Enum<?> = true is a Object = true Reifiable interface types - Gold: is Serializable = true is Comparable<?> = true is Constable = true Non-reifiable types: gold is an Enum<Rank> = true gold is an Enum<Color> = false gold is an Comparable<Rank> = true gold is an Comparable<Color> = false
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
►Enum Types and Enum Constants
Use 'class' to Define Enumeration
Instance Variables for Enum Constants
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