Gain an in-depth understanding of enum in Java and Javaenum
We can use either the public static final String constant or the enum String constant to represent a limited number of things in a program. In general, the former is simple, but it cannot provide more information. In Java, enum is very powerful and more professional.
1.The most C-style enum:
/*** Data source category: master/slave */public enum DataSources {MASTER0, MASTER1, SLAVE0, SLAVE1, SLAVE2, SLAVE2}
This is the simplest enum, almost the same as in C. It is simple, simple, but has weak functions.
2.Essence of enum
The essence of enum in Java isA class that inherits java. lang. Enum. So he is more powerful than the C-style enum. It can have attributes, methods, constructor, etc. The following is an example:
Import org. apache. commons. lang. stringUtils;/*** error code enumeration */public enum ErrorCodeEnum {/** system exception */SYSTEM_ERROR ("system_error", "system exception "), /** invalid parameter */ILLEGAL_ARGUMENT ("illegal_argument", "invalid parameter"),/** invalid signature */ILLEGAL_SIGN ("illegal_sign", "invalid signature "),
//......
/** Invalid registration code */ILLEGAL_REG_CODE ("illegal_reg_code", "invalid Registration code");/** enumeration code */private String code; /** enumeration Description */private String desc; private ErrorCodeEnum (String code, String desc) {this. code = code; this. desc = desc;} public String getCode () {return code;} public String getDesc () {return desc ;} /*** obtain the enumeration according to the enumeration code * @ param code enumeration code * @ return enumeration */public static final ErrorCodeEnum getByCode (String code) {if (StringUtils. isBlank (code) {return null;} for (ErrorCodeEnum item: ErrorCodeEnum. values () {if (StringUtils. equals (item. getCode (), code) {return item ;}} return null ;}}
We can see that ErrorCodeEnum has the String code and String desc attributes and has a private constructor. The reason is that our enumerated constants need to use the definition of this private constructor: SYSTEM_ERROR ("system_error", "system exception") is the private constructor called for enumeration: private ErrorCodeEnum (String code, String desc); in fact, the enumerated constants SYSTEM_ERROR and ILLEGAL_ARGUMENT defined in ErrorCodeEnum are actually equivalent to an ErrorCodeEnum instance, they are generated by calling the private constructor of ErrorCodeEnum. The attribute String code and String desc of ErrorCodeEnum are defined to provide more detailed error information. In addition, you can define other auxiliary methods in the enumeration ErrorCodeEnum.
Therefore, enumeration is essentially a class inherited from java. lang. Enum. Enumeration constants are enumeration instances.. Enumeration can have attributes and methods to enhance the enumeration function. Enumeration is not very easy to understand in Java. Generally, it is difficult to understand the nature behind enumeration.
3.Common enumeration methods
public static void main(String[] args){ System.out.println(SYSTEM_ERROR.name()); System.out.println(SYSTEM_ERROR.ordinal()); System.out.println(SYSTEM_ERROR.toString()); for(ErrorCodeEnum e : ErrorCodeEnum.values()){ System.out.println(e.name()); System.out.println(e.getDesc()); } System.out.println(ErrorCodeEnum.SYSTEM_ERROR.name());
System.out.println(ErrorCodeEnum.valueOf(ErrorCodeEnum.class, "ILLEGAL_ARGUMENT")); }
String name (): Returns the name of this enum constant, exactly as declared in its enum declaration. Return the String when the enumerated constant is declared.
Int ordinal (): return the sequential position of the declared enumerated constant, starting from 0, like the index of the array.
ValueOf (Class <T> enumType, String name): it is actually a conversion from the String of the enumerated constant to the enumerated constant, which is equivalent to a factory method.
The name () method is a conversion from an enumerated constant to a string, while valueOf is a conversion from a string to an enumerated constant.
Values (): This method is an implicit method. All the constants of an enum type can be obtained by calling the implicitpublic static T[] values()
Method of that type. Used to traverse all enumeration constants in the enumeration.
4.Data structure related to enum: EnumMap. For details about EnumSet, refer to jdk documentation.
5.Advantages of enum over Constants)