0

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?

KnusperPudding
  • 313
  • 2
  • 10
  • In the first declaration, you trying to create a raw `ArrayList` by just using `Example()`. This will give you an error since you specified a type of `String`. In your second statement, you are using something called the diamond operator that was introduced in Java 7, `<>`. `` would also work. I suggest changing the return type of your `createList()` method to `ArrayList` – Logan Dec 06 '16 at 13:24
  • @LoganKulinski im aware of the diamond interface. But E should have nothing to do with the String. I dont want to use E in my createNewList() methode but else where. – KnusperPudding Dec 06 '16 at 13:32
  • @Bubletan i think thats the same issue. thanks! – KnusperPudding Dec 06 '16 at 13:37
  • strange issue... making the method `static` is another workaround ;-) – Roland Dec 06 '16 at 13:39
  • @Roland it really is. thanks for the addition. though in the example it would work but in my more complex class, it references to some values of the instance and cant be `static` – KnusperPudding Dec 06 '16 at 13:52

0 Answers0