InnerClass
is a non-static inner class. Non-static members of OuterClass
are within the scope of the type parameter T
of OuterClass
. This means that InnerClass
is implicitly generic.
Every time inside an instance context of OuterClass
where you write just InnerClass
, without explicitly qualifying it, it is implicitly treated as OuterClass<T>.InnerClass
. So when you wrote
InnerClass[] array = new InnerClass[4];
the compiler sees it as
OuterClass<T>.InnerClass[] array = new OuterClass<T>.InnerClass[4];
// ^
// see? you are using "new" to create an array of a parameterized type
Even though the parameter is not physically on InnerClass
, it is on the OuterClass
, it is still a type parameter of InnerClass
, just written in a different position.
Creating an array of a parameterized type, as you may know, is not allowed in Java. Just like how List<T>[] = new List<T>[4];
is not allowed.
So what's the usual workaround for creating an array of a generic type? You can create an array of the raw type:
List<T>[] = new List[4]; // legal
or of the wildcard-parameterized type:
List<T>[] = (List<T>[])new List<?>[4]; // legal
Now back to your problem of the inner class. What is the raw type in this case? It is not InnerClass
, because as we have seen, it is implicitly parameterized with T
. We have to explicitly qualify InnerClass
with a raw OuterClass
to get the raw InnerClass
:
InnerClass[] = new OuterClass.InnerClass[4]; // legal
Alternately using the wildcard-parameterized type (again, we have to put the wildcard on OuterClass
):
InnerClass[] = (InnerClass[])new OuterClass<?>.InnerClass[4]; // legal