-
Bug
-
Resolution: Fixed
-
P3
-
5.0
FULL PRODUCT VERSION :
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Consider the code below:
public enum EnumTest {
anEnumValue {
private final EnumTest thisOne = anEnumValue;
String getMessage() { return "Here is what thisOne gets assigned: " + thisOne; }
};
abstract String getMessage();
public static void main(String[] args) {
System.out.println(anEnumValue.getMessage());
}
}
This code compiles, but when you run it, you get the output:
Here is what thisOne gets assigned: null
What this means is that the final local variable thisOne defined as a constant-specific field inside the enum constant anEnumValue has been assigned null.
This is NOT the behavior that I would have expected: I would have thought that it would really pick up the non-null reference to anEnumValue, especially given that it compiles.
But if the semantics of enums in java do not support this behavior, then at least javac should fail with the error that this is an illegal forward reference, rather than compile and allow it to fail at runtime.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code above.
REPRODUCIBILITY :
This bug can be reproduced always.
CUSTOMER SUBMITTED WORKAROUND :
NONE, and I would really like to code using the technique illustrated above, as it is a great way to program state machines, which is something that enums really should be used for.
###@###.### 2004-12-17 00:39:14 GMT
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
A DESCRIPTION OF THE PROBLEM :
Consider the code below:
public enum EnumTest {
anEnumValue {
private final EnumTest thisOne = anEnumValue;
String getMessage() { return "Here is what thisOne gets assigned: " + thisOne; }
};
abstract String getMessage();
public static void main(String[] args) {
System.out.println(anEnumValue.getMessage());
}
}
This code compiles, but when you run it, you get the output:
Here is what thisOne gets assigned: null
What this means is that the final local variable thisOne defined as a constant-specific field inside the enum constant anEnumValue has been assigned null.
This is NOT the behavior that I would have expected: I would have thought that it would really pick up the non-null reference to anEnumValue, especially given that it compiles.
But if the semantics of enums in java do not support this behavior, then at least javac should fail with the error that this is an illegal forward reference, rather than compile and allow it to fail at runtime.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code above.
REPRODUCIBILITY :
This bug can be reproduced always.
CUSTOMER SUBMITTED WORKAROUND :
NONE, and I would really like to code using the technique illustrated above, as it is a great way to program state machines, which is something that enums really should be used for.
###@###.### 2004-12-17 00:39:14 GMT
- relates to
-
JDK-6424491 Cannot initialise nested enums
-
- Closed
-