Consider the following code :
class R2<E>
{
E a;
E get()
{
return a;
}
void set(E a)
{
this.a=a;
}
}
class R3
{
void doit(R2 a) // (1)
{
a.set(new Integer(5));
}
}
When I compile the file containing class R3, it rightly gives an unchecked warning because the compiler can't know for sure what was the element type of a (if any) i.e its a raw type. But if we replace (1) with :
void doit(R2<?> a)
In this case, the compiler complains. My question is that the wildcard ? denotes all types, so it should accept every type. The exact error is :
R3.java:7: set(capture#537 of ?) in R2<capture#537 of ?> cannot be applied to (java.lang.Integer)
a.set(new Integer(5));
^
1 error