Java Tutorials - Herong's Tutorial Examples - v8.22, by Herong Yang
Instance Variables for Enum Constants
This section provides a tutorial on how to define instance variables and methods for enum constants.
Since enum types are actually class types, we can define instance variables and methods to your enum types by following these rules:
Here is a sample program that shows you how to define instance variables and methods in an "enum" declaration statement. Notice that the "Rank" enum type is declared like an inner class inside the "EnumWithInstanceVariable" class.
/* EnumWithInstanceVariable.java * Copyright (c) HerongYang.com. All Rights Reserved. */ class EnumWithInstanceVariable { enum Rank { // enum constants declaration and initialization GOLD(500), SILVER(300), BRONZE(100); // instance variable public int point; // constructor Rank(int point) { this.point = point; } // instance method public int getPoint() { return point; } } 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); out.println("Getting enum constant class name:"); String clsName = Rank.GOLD.getClass().getName(); out.println(" Rank.GOLD.getClass().getName() = "+clsName); out.println("Accessing enum constant instance variable:"); out.println(" Rank.GOLD.point = "+Rank.GOLD.point); out.println(" Rank.SILVER.point = "+Rank.SILVER.point); out.println(" Rank.BRONZE.point = "+Rank.BRONZE.point); out.println("Accessing enum constant instance method:"); out.println(" Rank.GOLD.getPoint() = "+Rank.GOLD.getPoint()); out.println(" Rank.SILVER.getPoint() = "+Rank.SILVER.getPoint()); out.println(" Rank.BRONZE.getPoint() = "+Rank.BRONZE.getPoint()); } }
If you compile and run EnumWithInstanceVariable.java, you should get:
herong> java EnumWithInstanceVariable.java Converting enum constant to string: Rank.GOLD = GOLD Rank.SILVER = SILVER Rank.BRONZE = BRONZE Getting enum constant class name: Rank.GOLD.getClass().getName() = EnumWithInstanceVariable$Rank Accessing enum constant instance variable: Rank.GOLD.point = 500 Rank.SILVER.point = 300 Rank.BRONZE.point = 100 Accessing enum constant instance method: Rank.GOLD.getPoint() = 500 Rank.SILVER.getPoint() = 300 Rank.BRONZE.getPoint() = 100
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