I have a class
public class OrderedBox<T> {}
Compiler doesn't allow to create member/local variable like these.
OrderedBox<? extends T> testItems1 = new OrderedBox<? extends T>();
List<? extends T> testItems2 = new ArrayList<? extends T>();
Its understandable cause at runtime, it doesn't guarantee the type of objects (upper bounded by T) will be inserted and will defy the typesafety.
But it allows to create member/local variable like these. Why and How does it allow this ?
private List<OrderedBox<? extends T>> testItems = new ArrayList<OrderedBox<? extends T>>();
Note:
I have this doubt while going through http://www.onjava.com/pub/a/onjava/excerpt/javaian5_chap04/index1.html
Probable duplicates :
Creating new generic object with wildcard
Generics wildcard instantiation
But both of these questions provides the reasoning for the compilation failure of the 2 options. I couldn't understand why and how the last 1 is allowed.