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

Static nested class allowed to extend non-static inner class



      class InstanceFromStatic<T> {
          class Inner {}

          static void m(Inner i) {} // expected: error; actual: error

          static class Child extends Inner { // expected: error; actual: nothing
              Child() { new InstanceFromStatic().super(); }

      InstanceFromStatic.java:5: error: non-static class InstanceFromStatic.Inner cannot be referenced from a static context
          static void m(Inner i) {}
      InstanceFromStatic.java:8: warning: [unchecked] unchecked conversion
              Child() { new InstanceFromStatic().super(); }
        required: InstanceFromStatic<T>
        found: InstanceFromStatic
        where T is a type-variable:
          T extends Object declared in class InstanceFromStatic

      Because the outer class is generic, references to the non-static instance class Inner are treated as if they were references to InstanceFromStatic<T>.Inner. Since T cannot be referenced from a static context, this should be an error. javac detects the error in most cases, but fails to do so in the 'extends' clause of a static nested class; an unchecked conversion to InstanceFromStatic<T> is then performed in order to provide the enclosing instance (even though T is undefined in this context).

      Note that, if InstanceFromStatic is not generic, none of this is a problem, and the program (correctly) compiles without error.

      I tried to find the spec for this check, and wasn't able to turn anything up. It's possible there needs to be a corresponding spec clarification.

      This is a regression beginning with 7: javac 6u65 reports two errors, as expected.


          Issue Links



              • Assignee:
                dlsmith Dan Smith
                dlsmith Dan Smith
              • Votes:
                0 Vote for this issue
                1 Start watching this issue


                • Created: