Usage:
1 Packagecom.wjq.enums;2 3 //User Status4 //0: Normal status (registered)5 //1: Has been blocked (registered)6 Public enumUserState {7NORMAL (0), SEALED (1);8 9 Private Final intvalue;Ten One PrivateUserState (intvalue) { A This. Value =value; - } - the Public intGetValue () { - returnvalue; - } - + Public Static voidMain (string[] args) { - //the output is 1 . + System.out.println (UserState.SEALED.getValue ()); A } at}
Principle:
After the compiler compiles, the enumeration class becomes an abstract class, inheriting java.lang.Enum. Enumeration constants that are defined in enumerations become public static final properties, implemented through inner classes. At the same time, the type is the type of the abstract class, the name is the name of the enumeration constant, and the enumeration constant is initialized by a static code block. Enumerations cannot be copied through cloning and serialization and deserialization, guaranteeing that an enumerated constant is a singleton.
Resources
How Java enumeration types are implemented
Java enumeration Usage and rationale