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?

5
  • 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, <>. <String> would also work. I suggest changing the return type of your createList() method to ArrayList<E> Commented Dec 6, 2016 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. Commented Dec 6, 2016 at 13:32
  • @Bubletan i think thats the same issue. thanks! Commented Dec 6, 2016 at 13:37
  • strange issue... making the method static is another workaround ;-) Commented Dec 6, 2016 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 Commented Dec 6, 2016 at 13:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.