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

Spurious forward reference error with final var + instance variable initializer

    Details

    • Subcomponent:
    • Resolved In Build:
      b34
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Not verified

      Description

      This program wrongly generates an 'illegal forward reference' error (at obj's declaration...even if the error was justified, it should occur at obj's use) :-

      class Huh {
          Runnable r = new Runnable() {
              public void run() {
                  Object o = obj;
              }
          };
          final Object obj = r;
      }

      The following program is morally equivalent (the JLS treats instance variable initializers and instance initializers uniformly) and does not generate an error:

      class Huh {
          Runnable r;
          {
              r = new Runnable() {
                  public void run() {
                      Object o = obj;
                  }
              };
          }
          final Object obj = r;
      }
      Simpler test case, given the following program

      class Test {
          final int a = b;
          final int b = a;
      }

      the compiler should report only ONE error, instead of two, as it can be seen from the output:

      Test.java:33: illegal forward reference
          final int a = b;
                        ^
      Test.java:34: illegal forward reference
          final int b = a;
                        ^
      2 errors

      While the first error is correct, the second one is a bug, accordingly to JLS 8.3.2.3

        Attachments

          Activity

            People

            • Assignee:
              mcimadamore Maurizio Cimadamore
              Reporter:
              abuckley Alex Buckley
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: