Definition of Enumeration

public enum AccountActionTypeEnum {
LOGIN,
REGISTER,
EDIT\_INFO;

private AccountActionTypeEnum() {
}
}

Values are typically in uppercase letters, separated by commas.

Enumerated constants are guaranteed for type safety and convenience, and the compiler prompts us to improve if a type problem occurs. How can we use enumerated types in other classes

Public static void main (String [] args) {/ / direct reference AccountActionTypeEnum type = AccountActionTypeEnum. LOGIN; }

Principle of Enumeration Implementation

Now that we have a general understanding of the definition and simple use of enumerated types, let’s look at the basic implementation of enumerated types. In fact, after creating the enumerated type with the enum keyword and compiling it, the compiler will generate a related class for us that inherits the java.lang. enum class from the Java API, That is, an enumerated type created with the enum keyword is actually a class type when compiled and it inherits from the java.lang.enum class. Below we compile the previously defined AccountActionTypeEnum. Java and view the generated class files to verify this conclusion:

Javac AccountActionTypeEnum. Java # compiled Java files generated AccountActionTypeEnum. Class files

And then through the jad decompiled AccountActionTypeEnum. Class

. \ jad - sjava. \ AccountActionTypeEnum. Class # I here jad exe on AccountActionTypeEnum directory at the same level, if you have to join the environment variable can be jad directly

The result of the decompiled AccountActionTypeEnum. Class

// Jad V1.5.8g. Copyright 2001 Pavel Kouznetsov. // Jad Home Page: http://www.kpdus.com/jad.html // Decompiler options: packimports(3) // Source File Name: AccountActionTypeEnum. Java < br > public final class AccountActionTypeEnum extends Enum {/ / compiler to write a public static method AccountActionTypeEnum[] values() { return (AccountActionTypeEnum[])$VALUES.clone(); Public static AccountActionTypeEnum valueOf(String s) {return (AccountActionTypeEnum)Enum.valueOf(AccountActionTypeEnum, s); } // private AccountActionTypeEnum(String S, int I) {// call the constructor super(s, I); } // Define the class variable, . That is why we can directly to AccountActionTypeEnum LOGIN form used in the public static final AccountActionTypeEnum LOGIN; public static final AccountActionTypeEnum REGISTER; public static final AccountActionTypeEnum EDIT\_INFO; private static final AccountActionTypeEnum $VALUES[]; Static {LOGIN = new AccountactionTypeEnum (" Login ", 0); static {LOGIN = new AccountactionTypeEnum (" Login ", 0); REGISTER = new AccountActionTypeEnum("REGISTER", 1); EDIT\_INFO = new AccountActionTypeEnum("EDIT\_INFO", 2); $VALUES = (new AccountActionTypeEnum[] { LOGIN, REGISTER, EDIT\_INFO }); }}

You can see from the decompilated code that the compiler did help us generate an AccountactionTypeEnum class (note that this class is final and will not be inherited) and that this class inherits from the java.lang.Enum class, which is an abstract class. In addition, The compiler also helps us generate four instance objects of type AccountactionTypeEnum for each of the three Types and an array of Types defined in the enumeration. Note the compiler also generates for us two static methods that are values () and the valueOf (), we will see here, use the keyword enum definition of enumerated types, at compile time, will also be converted into a real class, in the class, there are each defined in the enumeration type variables corresponding instance of the object, Public static final AccountactionTypeEnum Login; , and the compiler creates two methods for the class, values() and valueOf(). Let’s take a closer look at the java.lang.enum class and the purpose of Values () and ValueOf ().

Enum abstract class common methods

Enum is the common base class for all Java language enumerated types (note that Enum is abstract), and here are its common methods:

The return type Method names Method statement
int compareTo(E o) Compare this enumeration to the order of the specified objects
boolean equals(Object other) Returns true if the specified object is equal to this enumeration constant.
Class<? > getDeclaringClass() Returns the Class object corresponding to the enumeration type of this enumeration constant
String name() Returns the name of this enumeration constant, which is declared in its enumeration declaration
int ordinal() Returns the ordinal number of an enumeration constant (its position in the enumeration declaration, where the initial constant ordinal is zero).
String toString() Returns the name of the enumeration constant included in the declaration
static<T extends Enum<T>> T static valueOf(Class<T> enumType, String name) Returns an enumeration constant of the specified enumeration type with the specified name.

The ordinal() method returns the order in which the enumeration variables are declared in the enumeration class. The index starts at 0. For example, the ordinal value of LOGIN in the AccountactionTypeEnum is 0. If the LOGIN declaration position changes, so does the value retrieved by the ordinal method. Note that this method should not be used in the first place in most cases, since it is always unpredictable. If we put the LOGIN in the second position, then it’s method of ordinal value will also change, now we will compile the LOGIN change place, and then to decompiled AccountActionTypeEnum. Class files.

New AccountActionTypeEnum. Class static blocks of code

static { REGISTER = new AccountActionTypeEnum("REGISTER", 0); // You can see that when we place LOGIN in the second position, its ordinal value becomes 1, and the value of 'ordinal()' becomes 1. LOGIN = new AccountActionTypeEnum("LOGIN", 1); EDIT\_INFO = new AccountActionTypeEnum("EDIT\_INFO", 2); $VALUES = (new AccountActionTypeEnum[] { REGISTER, LOGIN, EDIT\_INFO }); }

The values method returns the class variable $values [], which is an array of enumerated objects. We can use this method to walk through the three enumerations defined.

The valueOf method calls the static method valueOf of the parent Enum and returns the enumeration class variable corresponding to Name.

Such as:

AccountActionTypeEnum.valueOf("LOGIN") =>    AccountActionTypeEnum.LOGIN

Enum source

// Comparable public abstract class Enum\<E extends Enum> implements Comparable, Serializable { <br> private final String name; Public final String name() {return name; } private final int ordinal; Public final int ordinal() {return ordinal; } protected Enum(String name, int ordinal) {this.name = name; this.ordinal = ordinal; } public String toString() { return name; } public final boolean equals(Object other) { return this==other; } public final int compareTo(E o) {Enum other = (Enum)o; Enum self = this; if (self.getClass() ! = other.getClass() && // optimization self.getDeclaringClass() ! = other.getDeclaringClass()) throw new ClassCastException(); return self.ordinal - other.ordinal; } @SuppressWarnings("unchecked") public final Class getDeclaringClass() {// var var var var var var var var var var var; GetClass () is the Object method Class<? > clazz = getClass(); // Get the parent Class object reference Class<? > zuper = clazz.getSuperclass(); return (zuper == Enum.class) ? (Class)clazz : (Class)zuper; } <br> public static \<T extends Enum> T valueOf(Class enumType, String name) {/ / enumType. EnumConstantDirectory () to get is a collection of map, the key value is the name, // EnumConstantDirectory EnumConstantDirectory EnumConstantDirectory EnumConstantDirectory EnumConstantDirectory According to the class object for a map collection value T result = enumType. EnumConstantDirectory () get (name); if (result ! = null) return result; if (name == null) throw new NullPointerException("Name is null"); throw new IllegalArgumentException( "No enum constant " + enumType.getCanonicalName() + "." + name); } / /... Omit other useless methods}

Enum implements the Comparable interface, which is why compareTo comparisons are possible. There is also the Enum constructor, which can only be called by the compiler. After all, we can only define an enumeration using the Enum keyword, but the compiler can do the rest.