1) In Java, an enum is a special kind of class, with an instance that reprsents each value of the enum.

Example:

enum Card{HEART, DIAMOND, CLUB, SPADE}

public class EnumTest {
	public static void main(String[] args) {
		Card card = Card.CLUB;
		String card_str = "";
		switch(card) {
		case HEART:
			card_str = "HEART";
			break;
			
		case DIAMOND:
			card_str = "DIAMOND";
			break;
			
		case CLUB:
			card_str = "CLUB";
			break;
			
		case SPADE:
			card_str = "SPADE";
			break;
		}
		
		System.out.println(card_str);
	}
}

2) Before declaring any class members(or initialization blocks), and enum must first declare all of its enum constatns. Fore convenience, you’re allowed to have a comma after the last enum constant.

enum Suit {
	HEART, DIAMOND, CLUB, SPADE, ;
	public static int getSize(){ return 4; }
}

  An enum cannot be declared to extend another type because all enums implicitly extend java.lang.Enum, which we discuss a little later. Nor can any other type extend an enum (not even another enum), because all enum types act as if they are implicitly final. An enum can declare that it implements one or more interfaces -- in fact all enums are implicitly Serializable and Comparable.

  The possible members of an enum include all the class members: fields, methods, and nested types, including nested enums. The enum constants themselves are implicitly static fields with the same type as the enum.

Every enum type E has two static methods that are automatically generated for it by the compiler:

public static E[] values()

Returns an array containing each of the enum constants in the order in which they were declared.

public static E valueOf(String name)

Returns the enum constant with the given name. If the name does not match an enum constant name exactly then an IllegalArgumentException is thrown.

  An enum also cannot be explicitly declared final, though it acts as if it were final.

Example:

enum Card{HEART, DIAMOND, CLUB, SPADE}

public class EnumTest {
	public static void main(String[] args) {		
		Card[] cards = Card.values();
		for(int i = 0; i < cards.length; ++i) 
			System.out.println(cards[i]);
	}
}

Output:

HEART
DIAMOND
CLUB
SPADE

 

3) An enum can have constructors, for example:

enum Suit {
    CLUBS("CLUBS"),
    DIAMONDS("DIAMONDS"),
    HEARTS("HEARTS"),
    SPADES("SPADES");

    String name;
    Suit(String name) { this.name = name; }

    public String toString() { return name; }
}

There are three restrictions on the definition of an enum constructor:

  • All enum constructors are private. While you can use the private access modifier in the constructor declaration, by convention it is omitted. Private constructors ensure that an enum type cannot be instantiated directly.
  • The constructor cannot explicitly invoke a superclass constructor. The chaining to the super constructor is handled automatically by the compiler.
  • An enum constructor cannot use a non-constant static field of the enum.

  This last restriction requires a little explanation. Because each enum constant is a static field of the enum type, the constructors are executed during static initialization of the enum class. The enum constant declarations must be the first declarations in the type, so the constructors for these values will always be the first code executed during static initialization. Any other static fields will be initialized afterward. So if a constructor were to refer to a static (non-constant) field of the enum, it would see the default uninitialized value. This would nearly always be an error, and so it is simply disallowed.

 

4) All enum types implicitly extend java.lang.Enum,but no class is allowed to extend Enum directly.