Chapter 19. Enumerated Types -- Thinking in Java

1) You can step through the list of enum constants by calling values( ) on the enum. The values( ) method produces an array of the enum constants in the order in which they were declared, so you can use the resulting array in (for example) a foreach loop.

 

2) When you create an enum, an associated class is produced for you by the compiler. This class is automatically inherited from java.lang.Enum.

 

3) The ordinal( ) method produces an int indicating the declaration order of each enum instance, starting from zero. You can always safely compare enum instances using ==, and equals( ) and hashCode( ) are automatically created for you. The Enum class is Comparable, so there’s a compareTo( ) method, and it is also Serializable. If you call getDeclaringClass( ) on an enum instance, you’ll find out the enclosing enum class. The name( ) method produces the name exactly as it is declared, and this is what you get with toString( ), as well. valueOf( ) is a static member of Enum, and produces the enum instance that corresponds to the String name you pass to it, or throws an exception if there’s no match.

 

4) Except for the fact that you can’t inherit from it, an enum can be treated much like a regular class. This means that you can add methods to an enum. It’s even possible for an enum to have a main( ).

 

5) Notice that if you are going to define methods you must end the sequence of enum instances with a semicolon. Also, Java forces you to define the instances as the first thing in the enum. The constructor can only be private or package access and the compiler won’t let you use it to create any new instances once the enum definition is complete. You must specify the constructor arguement in the brackets right after where you define the enum instance.

 

6) Although normally you must qualify an enum instance with its type, you do not have to do this in a case statement.

 

7) There is no values( ) method in the Enum's definition. values( ) is a static method that is added by the compiler. There is a getEnumConstants( ) method in Class, so even if values( ) is not part of the interface of Enum, you can still get the enum instances via the Class object.

 

8) We’ve established that all enums extend java.lang.Enum. Since Java does not support multiple inheritance, this means that you cannot create an enum via inheritance. However, it is possible to create an enum that implements one or more interfaces.

 

9) The EnumSet was added to Java SE5 to work in concert with enums to create a replacement for traditional int-based "bit flags." The EnumSet is designed for speed, because it must compete effectively with bit flags (operations will be typically much faster than a HashSet). Internally, it is represented by (if possible) a single long that is treated as a bit-vector, so it’s extremely fast and efficient.

 

10) An EnumMap is a specialized Map that requires that its keys be from a single enum. Because of the constraints on an enum, an EnumMap can be implemented internally as an array. Thus they are extremely fast, so you can freely use EnumMaps for enum-based lookups. You can only call put( ) for keys that are in your enum, but other than that it’s like using an ordinary Map. Just as with EnumSet, the order of elements in the EnumMap is determined by their order of definition in the enum.

 

11) Java enums have a very interesting feature that allows you to give each enum instance different behavior by creating methods for each one. To do this, you define one or more abstract methods as part of the enum, then define the methods for each enum instance. This is often called table-driven code. The syntax for defining a constant-specific method is effectively that of an anonymous inner class, but more succinct.

 

12) Each enum element is actually a static final instance of the enum type. Also, because they are static, enum instances of inner enums do not behave like ordinary inner classes; you cannot access non-static fields or methods in the outer class. ( In this case , inner enum is a static inner class.)

你可能感兴趣的:(java,Access)