title: Java generic list return type
body: <p>I'm having some problems with a method returning a generic list. The code is basically this:</p>
  
  <pre><code>public class MyClass{
      private List&lt;MyListElement&gt; myList = new ArrayList&lt;MyListElement&gt;();
  
      public &lt;E&gt; List&lt;E&gt; getGenericList(){
          return new ArrayList&lt;E&gt;();
      }
  
      public void thisWorks(){
          List&lt;MyListElement&gt; newList = getGenericList();
          myList.addAll(newList);
      }
  
      public void thisDoesntWork(){
          myList.addAll(getGenericList());
      }
  
      public void thisDoesntWorkEither(){
          for(MyListElement elem : getGenericList()){
              fiddle();
          }
      }
  }
  </code></pre>
  
  <p>Why does the <code>thisDoesntWork()</code> method not work, and is there any other way around it (other than doing it the <code>thisWorks()</code> way which isn't always practical)?</p>