This is how you can avoid the cast with static methods:
public class MyClass {
public static List<? extends A> myMethod(List<? extends A> a) {
return a;
}
public static void main(String[] args) {
List newList = new ArrayList<A>();
List<?> newList2 = new ArrayList<A>();
List<B> oldList = new ArrayList<B>();
newList = MyClass.myMethod(oldList);
newList2 = MyClass.myMethod(oldList);
}
}
In the code above, B extends A. When newList variable is defined as List without generics or as List with wildcard type (List< ? >) cast is not necessary. On the other hand if you only want to get rid the warning you can use '@SuppressWarning' annotation. Check this link for more info What is SuppressWarnings ("unchecked") in Java?
Here is simple example for @SuppressWarnings ("unchecked"):
public static List<? extends A> myMethod(List<? extends A> a) {
// …
}
@SuppressWarnings ("unchecked")
newAList = (List<A>) MyClass.myMethod(oldAList);