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

Regression: Cannot instantiate an inner class of a type variable

XMLWordPrintable

    • b70
    • generic
    • generic
    • Verified

      I am wondering if the following program is correct or wrong.
      Javac 1.5 did allow it, Javac 1.6 rejects it.

      The spec seems to imply it is legitimate: In 4.4, it claims:
      "The members of a type variable X with bound T & I1 ... In are the members
      of the intersection type (§4.9) T & I1 ... In appearing at the point where
      the type variable is declared."

      Then in 4.9, last bullet:
      "• Then the intersection type has the same members as a class type (§8)
      with an empty body, direct superclass Ck and direct superinterfaces IT1 , ..., ITn,
      declared in the same package in which the intersection type appears."

      -------------

      public class Outer {
        public class Inner {}
      }

      class ATest<T extends Outer> {
         public ATest() {
            T instance = makeInstance();
            Outer.Inner peq = instance.new Inner(); // javac 1.6 error: "cannot select from a type variable"
         }

         private T makeInstance() {
            return (T) new Outer();
         }
      }

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: