Enum Constant Inherited Methods

This section provides a tutorial on how to invoke static and instance method inherited from the generic base class of java.lang.Enum.

From previous tutorial, we learned that all enum types have a common super type of java.lang.Enum<?> supported by the generic base class of java.lang.Enum<E extends Enum*lt;E>>. The base class implements 3 interfaces: Serializable, Comparable<E>, Constable. It also provided several interesting static and instance methods:

Here is a sample program that shows you how to invoke methods listed above:

/* EnumInheritedMethods.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.lang.Enum;
class EnumInheritedMethods {
   enum Rank {
      GOLD, SILVER, BRONZE;
   }

   public static void main(String[] arg) {
      java.io.PrintStream out = System.out;
      Rank gold = Rank.GOLD;
      Rank silver = Rank.SILVER;

      out.println("Inherited instance methods:");
      out.println("   silver.name() = "+silver.name());
      out.println("   silver.toString() = "+silver.toString());
      out.println("   silver.ordinal() = "+silver.ordinal());
      out.println("   silver.compareTo() = "+silver.compareTo(gold));
      out.println("   silver.getClass() = "+silver.getClass());
      out.println("   silver.getDeclaringClass() = "
         +silver.getDeclaringClass());

      out.println("Base static methods:");
      out.println("   Enum.valueOf() = "+Enum.valueOf(Rank.class, "GOLD"));
   }
}

If you compile and run EnumInheritedMethods.java, you should get:

C:\herong> java EnumInheritedMethods.java

Inherited instance methods:
   silver.name() = SILVER
   silver.toString() = SILVER
   silver.ordinal() = 1
   silver.compareTo() = 1
   silver.getClass() = class EnumInheritedMethods$Rank
   silver.getDeclaringClass() = class EnumInheritedMethods$Rank
Base static methods:
   Enum.valueOf() = GOLD

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

 Outdated Tutorials

 References

 Full Version in PDF/EPUB