2009-08-12 9 views
7

すべてのJava列挙には静的な値を有している()メソッドは、このメソッドが定義されている場合しかし、私は理解できないこのEnum.values()はどこに定義されていますか?

for (MyEnum enum : MyEnum.values()) { 
    // Do something with enum 
} 

ように使用することができます。 Javadocにそれについて言及されておらず、ソースファイルのどこにも表示されません。

答えて

9

で定義されています:valuesvalueOfは暗黙的にすべての列挙型に宣言されます。

/** 
* Returns an array containing the constants of this enum 
* type, in the order they're declared. This method may be 
* used to iterate over the constants as follows: 
* 
* for(E c : E.values()) 
*  System.out.println(c); 
* 
* @return an array containing the constants of this enum 
* type, in the order they're declared 
*/ 
public static E[] values(); 

/** 
* Returns the enum constant of this type with the specified 
* name. 
* The string must match exactly an identifier used to declare 
* an enum constant in this type. (Extraneous whitespace 
* characters are not permitted.) 
* 
* @return the enum constant with the specified name 
* @throws IllegalArgumentException if this enum type has no 
* constant with the specified name 
*/ 
public static E valueOf(String name); 

これらのメソッドは、コンパイル時に追加され、あなたが使用している場合ので、 javapコードを逆アセンブルするには、実際に自分の体を見ることができます。

2

Java配列のlengthプロパティが定義されていないのと同じように、明示的に定義されていません。これは、具体的な列挙型に対して暗黙的に利用可能です。

関連する問題