这是一个枚举类Day.java
public enum Day { MONDAY("星期一"), TUESDAY("星期二"), WEDNESDAY("星期三"), THURSDAY("星期四"), FRIDAY("星期五"), SATURDAY("星期六"), SUNDAY("星期天"); /** * 中文描述 */ private String desc; public String getDesc() { return desc; } /** * 私有构造方法,防止被外部调用 * @param desc */ private Day(String desc) { this.desc = desc; }}
进入到这个文件目录下编译该文件:javac -encoding UTF-8 Day.java
这时候,该目录会有一个Day.class编译后的字节码文件;
再执行:javap -p Day.class
Compiled from "Day.java"public final class Day extends java.lang.Enum{ public static final Day MONDAY; public static final Day TUESDAY; public static final Day WEDNESDAY; public static final Day THURSDAY; public static final Day FRIDAY; public static final Day SATURDAY; public static final Day SUNDAY; private java.lang.String desc; private static final Day[] $VALUES; public static Day[] values(); public static Day valueOf(java.lang.String); public java.lang.String getDesc(); private Day(java.lang.String); static {};}
可以看到,public enum Day定义一个枚举类,反编译之后看到public final class Day extends java.lang.Enum<Day>Java枚举类不能被继承;
每一个枚举都是public static final只能被赋值一次,大概就是这些,如果想看更加详细的反编译可执行:javap -c Day (这里不要写文件后缀.class);
反编译可以输出常量池的信息以及元素个数:javap -verbose Day.class
Java枚举的简单例子: