Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java generics type conversion

Tags:

java

Here's the sample code:

public static BaseListAdapter<? extends Item> getListAdapter() {
    ...
}

...

public class MyClass<T extends Item> {
    ...
    BaseListAdapter<T> adapter = (BaseListAdapter<T>) getListAdapter();
    ...
}

The compiler complains that there's an unchecked cast.

java: unchecked cast
  need: BaseListAdapter<T>
  found:    BaseListAdapter<capture#1, ? extends Item>

I want to know why this warning is produced and how to resolve it.

like image 671
Lion Avatar asked Sep 10 '26 21:09

Lion


1 Answers

You probably want

public static <T extends Item> BaseListAdapter<T> getListAdapter() {
like image 171
Thilo Avatar answered Sep 13 '26 10:09

Thilo