Reputation: 1815
Gen <Integer> iOb = new Gen <> (50, n); //Works good
Gen <Integer> gens[] = new Gen <> [10]; //Error
Gen <?> gen[] = new Gen <?> [10]; //Alternative way for the second form
I want to know why the 1st and 3rd declarations works fine, but the second one does not.
What the difference between the three?
Upvotes: 0
Views: 59
Reputation: 178303
The second and third lines are examples of creating generic arrays. The second one is not reifiable. That means that the type isn't available at runtime.
The JLS, Section 15.10, covers array creation:
It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type (§4.7).
Section 4.7 defines a reifiable type:
A type is reifiable if and only if one of the following holds:
It refers to a non-generic class or interface type declaration.
It is a parameterized type in which all type arguments are unbounded wildcards (§4.5.1).
It is a raw type (§4.8).
It is a primitive type (§4.2).
It is an array type (§10.1) whose element type is reifiable.
It is a nested type where, for each type T separated by a ".", T itself is reifiable.
So, the second line is disallowed because it's an array type of a type that's not reifiable, because it's generic and it's not all unbounded wildcards. The third line is good only because its generics is all unbounded wildcards.
The first line is ok because it's not an array.
Upvotes: 2