Wildcard (Java): Difference between revisions

Content deleted Content added
Bounded Wildcards: elaborate/improve exposition
Line 43:
 
== Object Creation with Wildcard ==
No objects may be created with a wildcard type parameter: <code>'''new''' Generic<?>()</code> is forbidden because <code>Generic<?></code> is abstract.<ref>{{citation|title=Java Language Specificatios|section=Class Instance Creation Expressions|url=http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.9|publisher=Oracle}}</ref> In practice, this is unnecessary because if one wanted to create an object that was assignable to a variable of type <code>Generic<?></code>, one could simply use any arbitrary type (that falls within the constraints of the wildcard, if any) as the type parameter.
 
However, <code>new List<Generic<?>>()</code> is allowed, because the wildcard is not a parameter to the instantiated type <code>List</code>. The same holds for <code>new List<List<?>>()</code>.