I am using a generics wildcard for my class and also using the wildcard of list in one of my methods:
public class Example<E> {
private E element;
private ArrayList<String> createNewList() {
return new ArrayList<>();
}
}
when i try to use the Method:
List<String> myList = new Example().createNewList();
tells me when compiling with Xlint:unchecked
unchecked conversion
required: List<String>
found: ArrayList
since my E-Type has nothing to do with the my createNewLists Generics < String>, why it tells me i dont have the required Type?
thanks in advance.
by the way - using:
List<String> myList = new Example<>().createNewList();
works perfectly fine. but why?
ArrayListby just usingExample(). This will give you an error since you specified a type ofString. In your second statement, you are using something called the diamond operator that was introduced in Java 7,<>.<String>would also work. I suggest changing the return type of yourcreateList()method toArrayList<E>staticis another workaround ;-)static