title: Java 8 Lambda variable scope
body: <p>I have 2 code samples:</p>
  
  <pre><code>int[] idx = { 0 };
  List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  list.add("abc");
  list.add("def");
  list.add("ghi");
  list.stream().forEach(item -&gt; {
      System.out.println(idx[0] + ": " + item);
      idx[0]++;
  });
  </code></pre>
  
  <p>Working properly.</p>
  
  <p>While this code has compile error:</p>
  
  <pre><code>int idx = 0;
  List&lt;String&gt; list = new ArrayList&lt;String&gt;();
  list.add("abc");
  list.add("def");
  list.add("ghi");
  list.stream().forEach(item -&gt; {
      System.out.println(idx + ": " + item);
      idx++;
  });
  </code></pre>
  
  <p>Saying:</p>
  
  <pre><code>Local variable idx defined in an enclosing scope must be final or effectively final.
  </code></pre>
  
  <p>The only difference is <code>idx</code> int or int array.</p>
  
  <p>What's the root cause?</p>