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

Invalid error when implicitly referencing enclosing type

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 1.4.2
    • 1.4.0, 1.4.2
    • tools
    • mantis
    • generic
    • generic, solaris_8
    • Verified

    Description

      This is a correction to 4531312:

      As pointed out by the Java Spec Report,
      http://www.ergnosis.com/java-spec-report/java-language/jls-8.8.5.1-d.html,
      it is legal to reference enclosing types in an explicit constructor invocation.
       For example, this compiles successfully with javac:

      $ cat C.java
      class C {
        C(Object o) {}
        private class Middle extends C {
          Middle(int i) {
            super(null);
          }
          Middle() {
            super(C.this.new Middle(1).new Inner(){});
          }
          class Inner {}
        }
      }
      $ javac C.java
      $

      However, remove the reference to C.this, and the compiler throws up its hands.

      $ cat C.java
      class C {
        C(Object o) {}
        private class Middle extends C {
          Middle(int i) {
            super(null);
          }
          Middle() {
            super(/*C.this.*/new Middle(1).new Inner(){}); // only this line changed
          }
          class Inner {}
        }
      }
      $ javac C.java
      C.java:8: cannot reference this before supertype constructor has been called

            super(/*C.this.*/new Middle(1).new Inner(){}); // only this line changed
                             ^
      1 error
      $

      This goes against JLS 15.9.2: Middle is an inner member class, the class
      instance creation expression is unqualified, so bullet 3.1.2 applies: the
      innermost class O which contains Middle as a member is C, so the enclosing
      instance i should be C.this, and the example should compile identically to the
      previous example.

      Attachments

        Issue Links

          Activity

            People

              gafter Neal Gafter
              gafter Neal Gafter
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: