Enum is a reference type(similar to a class, interface and array), which holds a reference to memory in the heap. Enum class can include other component of a traditional class, such as constructors, member variables and methods.(This is where Java's enum is more powerful than C/C++'s counterparty). Each enum constant can be declared with parameters to be passed to the constructor when it is created.
Enum默认是public static final的,必须是private constructor,所以不可以instantiate enum,直接用TrafficLight.RED。Enum定义中第一行一定要是RED(30), AMBER(10)...,然后再是private final seconds...。Enum用方法.values()返回数组{RED, AMBER, GREEN}。
For example:
enum TrafficLight {
RED(30),
AMBER(10),
GREEN(20);
private final seconds;
TrafficLight(int seconds){
this.seconds=seconds;
}
int getSeconds(){
return this.seconds;
}
}
Enum constants, like RED, are all static. So you can use TrafficLight.RED.