title: Why don't instance fields need to be final or effectively final to be used in lambda expressions?
body: <p>I'm practicing lambda expressions in Java. I know local variables need to be final or effectively final according to the Oracle documentation for Java SE 16 <a href="https://docs.oracle.com/javase/specs/jls/se16/html/jls-15.html#jls-15.27.2" rel="noreferrer">Lambda Body </a>:</p>
  <blockquote>
  <p>Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be final or effectively final (§4.12.4), as specified in §6.5.6.1.</p>
  </blockquote>
  <p>It doesn't say why though. Searching I found this similar question <a href="https://stackoverflow.com/questions/50456920/why-does-variables-in-lambdas-have-to-be-final-or-effectively-final">Why do variables in lambdas have to be final or effectively final?</a>, where StackOverflow user &quot;snr&quot; responded with the next quote:</p>
  <blockquote>
  <p>Local variables in Java have until now been immune to race conditions and visibility problems because they are accessible only to the thread executing the method in which they are declared. But a lambda can be passed from the thread that created it to a different thread, and that immunity would therefore be lost if the lambda, evaluated by the second thread, were given the ability to mutate local variables.</p>
  </blockquote>
  <ul>
  <li>Source: <a href="http://www.lambdafaq.org/what-are-the-reasons-for-the-restriction-to-effective-immutability/" rel="noreferrer">Why the restriction on local variable capture?</a></li>
  </ul>
  <p>This is what I understand: a method can only be executed by one thread (let's say thread_1) at a time. This ensures the local variables of that particular method are modified only by thread_1. On the other hand, a lambda can be passed to a different thread (thread_2), so... if thread_1 finishes with the lambda expression and keeps executing the rest of the method it could change the values of the local variables, and, at the same time, thread_2 could be changing the same variables within the lambda expression. Then, that's why this restriction exists (local variables need to be final or effectively final).</p>
  <p>Sorry for the long explanation. Am I getting this right?</p>
  <p>But the next questions would be:</p>
  <ul>
  <li>Why isn't this case applicable to instance variables?</li>
  <li>What could happen if thread_1 changes instance variables at the same time as thread_2 (even if they are not executing a lambda expression)?</li>
  <li>Are instance variables protected in another way?</li>
  </ul>
  <p>I don't have much experience with Java. Sorry if my questions have obvious answers.</p>