list is of type List<?> and that means list can reference a list-object of type List<String>, of type List<Number> or even a list-object of type List<
Thread>. It can reference
every list-object you want it to.
Now you go and say: "hey compiler, I am going to add an apple into the list-object referenced by list". No wonder, the compiler won't let you, because he cannot be sure, if that operation is type-safe. If you are absolutely sure, that the list-object referenced by list is indeed able to hold apples, than you have to tell the compiler that. You could do so with an explicit cast, like:
((List< ? super Apple > ) list).add(new Apple())
telling him, "the object in heap that list is referring to is a list-Object of Apples or maybe Fruits, I am not sure, but I am sure, that it can hold apples"
[ May 22, 2007: Message edited by: Sasha Ruehmkorf ]
[ May 22, 2007: Message edited by: Sasha Ruehmkorf ]