This is the first day of my participation in the More text Challenge. For details, see more text Challenge

According to Chapter 6, Enumerations and Annotations of [Effective Java], an enumeration type is a type that consists of a fixed set of constants that constitute a legal value.

Talk is Cheap, Show me the Code!

Typical enumeration template

The following is a personal enum template.

The code is divided into n parts:

  1. All enumeration variables are as shown in the code:

        TO_PAY, TO_DELIVER, TO_RECEIVE, FINISHED;
    Copy the code
  2. Instance domain of the enumeration

    private final int val;
    private final String name;
    Copy the code

    Enumerations are inherently immutable and can easily be used with the “==” operator. All instance fields of an enumeration should be set to final.

    Also, be careful: never export the value associated with an enumeration based on its ordinal number, but save it in an instance field [Effective Java] [3rd edition] P143.

  3. The way other types are converted to and from enumerated types

    /** * Enumeration to int *@return* /
    public int getVal(a) {
    	return val;
    }
        
        /** *@param val
     * @return* /
    public static OrderState getOrderState(int val) {
    	for (OrderState orderState : OrderState.values()) {
    		if (orderState.val == val) {
    			returnorderState; }}throw new RuntimeException("Wrong order status");
    }
        
    Copy the code

    In particular: the method values(), which returns an enumerated array of all values in order.

  4. Abstract methods

    This approach helps us implement the strategy pattern.

    With the abstract method, we can avoid using switk-case statements to avoid forgetting to add special operations to the enumeration values when we add them later. You can also use switch-case in enumerations.

    [Effective Java] : “Switch-case statements in an enumeration are appropriate for adding constant-specific behavior to external enumeration types.”

    In addition, according to Article 38 [Third Edition] of [Effective Java], we can simulate extensible enumerations with interfaces.

/** * Order status */
public enum OrderState {
	TO_PAY("Pending payment".0) {
		@Override
		public boolean deliver(a) {
			return false;
		}
	},
	TO_DELIVER("Goods to be shipped".1) {
		@Override
		public boolean deliver(a) {
			return true;
		}
	},
	TO_RECEIVE("Goods to be received".2) {
		@Override
		public boolean deliver(a) {
			return false;
		}
	},
	FINISHED("Done".3) {
		@Override
		public boolean deliver(a) {
			return false; }};/** * enumerations are inherently immutable. All fields should be declared final */
	private final int val;
	private final String name;
	
	/** * private modifier to prevent external calls *@param name
	 * @param val
	 */
	private OrderState(String name, int val) {
		this.name = name;
		this.val = val;
	}

	/** * Enumeration to int *@return* /
	public int getVal(a) {
		return val;
	}

	public String getName(a) {
		return name;
	}
	
	/** *@param val
	 * @return* /
	public static OrderState getOrderState(int val) {
		for (OrderState orderState : OrderState.values()) {
			if (orderState.val == val) {
				returnorderState; }}throw new RuntimeException("Wrong order status");
	}
	
	/** * Abstract method: shipping *@return* /
	public abstract boolean deliver(a);
	
}

Copy the code
When to use enumerations

Enumerations should be used whenever a set of fixed constants is needed and its members are known at compile time. However, the set of constants in an enumeration type does not have to always remain the same.

The other EnumMap I don’t use very often is not described.

Small flaws in enumerations

The space and time cost of loading and initializing enumerations is negligible.