Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6209839

Illegal forward reference to enum constants allowed by javac

    Details

    • Subcomponent:
    • Introduced In Build:
      5.0
    • Introduced In Version:
      5.0
    • Resolved In Build:
      b65
    • CPU:
      x86
    • OS:
      windows_xp
    • Verification:
      Verified

      Description

      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

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                ahe Peter Ahe
                Reporter:
                rmandalasunw Ranjith Mandala (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: