title: How to instantiate nested Lists in java? list<list<Integer>>
body: <p>Edit:</p>
  
  <p>Thanks for pointing me to the generic type invariant discussion. Looks like the problem is at the generic type part. <code>List&lt;E&gt;</code> where <code>E</code> is a generic type, in this case, <code>E</code> is <code>List&lt;Integer&gt;</code> which is an interface with <code>Integer</code>s as elements, and <code>LinkedList&lt;integer&gt;</code> is an object (with <code>Integer</code>s as elements) not an interface. So it does not work.</p>
  
  <p>So this needs to be done in two steps. Am I understanding this correctly?</p>
  
  <pre><code>List&lt;List&lt;Integer&gt;&gt; outter_list = new LinkedList&lt;List&lt;Integer&gt;&gt;();
  List&lt;Integer&gt; innder_list = new LinkedList&lt;Integer&gt;();
  </code></pre>
  
  <p>It's somewhat different than the parent child classes case. Since in this case, we are comparing an object implements the interface to the underlying interface.</p>
  
  <p>I hope they would make this easier...</p>
  
  <p>Original question:</p>
  
  <p>This is from questions on LeetCode. A question may require you to write a method that returns a <code>List&lt;Integer&gt;</code>. I could do:</p>
  
  <pre><code>LinkedList&lt;Integer&gt; list;
  //code to construct the list here.
  return list;
  </code></pre>
  
  <p>And it works fine.</p>
  
  <p>There is a question that requires to return a <code>List&lt;List&lt;Integer&gt;&gt;</code> and below</p>
  
  <pre><code>LinkedList&lt;LinkedList&lt;Integer&gt;&gt; list;
  //code to construct the lists here.
  return list;
  </code></pre>
  
  <p>this would not work and resulted in incompatible type error. I am curious why.</p>
  
  <p>I had to use <code>LinkedList&lt;List&lt;Integer&gt;&gt;</code> in the first line of declaration in order for it to work. As to my understanding the <code>LinkedList&lt;LinkedList&lt;Integer&gt;&gt; list;</code> is a perfect fine object that implements <code>List&lt;List&lt;Integer&gt;&gt;</code>. Why is it incompatible?</p>