I have the following generic Java class:
class A<T> {
List<A> list;
List<A> getList() {
return list;
}
}
When I try to get the first element of the list
A a = new A();
A b = a.getList().get(0);
I have the compiler error:
Type mismatch: cannot convert from Object to A
If I introduce an intermediary list:
A a = new A();
List<A> sameList = a.getList();
A b = sameList.get(0);
the error disappears, but the warning appears:
Type safety: The expression of type List needs unchecked conversion to conform to List<A>
The variable sameList
has the same type as the method getList()
. I am ready to put up with it, but I want to understand the reason. Why is there is a conversion?