Effective Java summary By Doo and Cheolho

Effective Java 3/E summary chanpter 3 (Shorter version)

Chanpter 5 : Enum and Annotation

You can find the longer version of this summary here.

Enum

  • Enum : instance control (declared as public static final), singleton, type stability
  • Method and field can be added to enum type. -> Receives data from the constructor and stores it in the instance (adds a value when creating each enum field)
public enum Operation {
    PLUS("+") {
        double apply(double x, double y) {
            return x + y;
        }
    },
    MINUS("-") {
        double apply(double x, double y) {
            return x - y;
        }
    },
    TIMES("*") {
        double apply(double x, double y) {
            return x * y;
        }
    },
    DIVIDE("/") {
        double apply(double x, double y) {
            return x / y;
        }
    };
    private final String symbol;

    Operation(String symbol) {
        this.symbol = symbol;
    }

    @Override
    public String toString() {
        return symbol;
    }
    public abstract double apply(double x, double y);
}
  • Use EnumSet instead of bit fields
  • Use EnumMap instead of ordinal indexing
  • Emulate extensible enums with interfaces

Precautions for Enum

  • Prohibit use of ordinal(). Instead, it is marked as an instance field

Annotation

  • Used for an explicit purpose rather than a function.
  • Always use @Override when overriding
  • If you need a type definition functionality, use the marker interface.