Domanda

Let me, please, ask for an explanation.

Inside the method doit() I can intantiate the generic nested class In<T>

public class A {

  public static class In<T> {
  }

  public static <X> void doit() {
    new In<X>();
  }
}

I can, of course, also reach any members of the class In<T>

public class A {

  public static class In<T> {
    public static class Inner<U> {
    }
  }

  public static <X> void doit() {
    new In.Inner<X>();
  }
}

I can still reach members of the class In<T> from the method doit() when both class and method are nested inside another class Container

public class A {

  public static class Container {

    public static class In<T> {

      public static class Inner<U> {
      }
    }

    public static <X> void doit() {
      new In.Inner<X>();
    }
  }
}

However, making A generic, as in

public class A<V> {

  public static class Container {

    public static class In<T> {

      public static class Inner<U> {
      }
    }

    public static <X> void doit() {
      new In.Inner<X>();
    }
  }
}

the compiler excepts an error: "The member type A.Container.In must be parameterized, since it is qualified with a parameterized type"

Could you, please, give me an explanation?

Notice that in the previous code classes and method are static.

Notice also that making generic the class Container, as in

public class A<V> {

  public static class Container<Z> {

    public static class In<T> {

      public static class Inner<U> {
      }
    }

    public static <X> void doit() {
      new In.Inner<X>();
    }
  }
}

The code compiles.

And compiles also the following code where Container is no longer generic but the call of the constructor of the class Inner<U> is now the more qualified Container.In.Inner<X>()

public class A<V> {

  public static class Container {

    public static class In<T> {

      public static class Inner<U> {
      }
    }

    public static <X> void doit() {
      new Container.In.Inner<X>();
    }
  }
}

Thank you.

È stato utile?

Soluzione

A nested class, being a static member of a class, does not depend on the (instance) type parameter of the class. As such, in your example

class A<V> {

    public static class Container {

        public static class In<T> {

            public static class Inner<U> {
            }
        }

        public static <X> void doit() {
            new In.Inner<X>(); // compilation error
        }
    }
}

There is absolutely no reason that the class instance creation expression

new In.Inner<X>()

would cause the error

"The member type A.Container.In must be parameterized, since it is qualified with a parameterized type"

The Inner member type is a nested class of In, which is a nested class of Container, which is a nested class of A. None of them have any relation to the type parameter declared in their declaring class.

This seems like a bug in your IDE and I would report it as such.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top