|
I hope the following concepts could help for java enum.
1. enum is a class which implicitly extends the java.lang.enum.
2. A is a static instance of enum type XX.
3. The actual value of A is "A", but A is a complex object, A is not a constant (public static String).
4. A instance of enum type XX can only compare with instances of XX, comparing with other type is not making sense regardless of the value of the enum, eg. Comparing "XX.A" with "XX2.A". (switch statement is a kind of comparison).
5. Within the switch block, the qualifier "XX" is not necessary because instance "a" can only compare with any XX instances which are defined in the enum {} block. If you try "case D" or "case XX2.A" (already define a XX2 enum), both are illegal. I guess this is a hard rule set in the compiler.
I think the compiler try to reduce the rumtime error for programmers.
6. Imagine the implementation of enum XX is like the following, but of course not fully equivalent
public class XX entends java.lang.enum {
public static final XX A = new XX("A");
public static final XX B = new XX("B");
public static final XX C = new XX("C");
// an array to support positioning
private XX() {};
private XX(String s){....};
.......
}
7. In "XX a = XX.A;", the XX qualifier is necessary base on the 6. because you are using a "public static final" from XX class.
8. You can consider the switch block is a kind of special case or a bonus the compiler offers. (no need the XX qualifier).
[ 本帖最后由 felix100 于 2-7-2009 19:06 编辑 ] |
|