title: How does java lambdas external references work?
body: <p>I'm wondering how does lambdas external references work. Let me explain:</p>
  
  <p>Suppose i have this supplier implementation and this model class :</p>
  
  <pre><code>public class TestSupplierImpl implements Supplier&lt;Boolean&gt; {
  
      public Predicate&lt;Integer&gt; predicate;
      public TestSupplierModel model;
  
      public TestSupplierImpl() {
          this.predicate = i -&gt; model.something.equals(i);
      }
  
      @Override
      public Boolean get() {
          return predicate.test(3);
      }
  }
  
  
      class TestSupplierModel {
          public Integer something;
  
          public TestSupplierModel(Integer something) {
              this.something = something;
          }
      }
  </code></pre>
  
  <p>Then i execute the following code:</p>
  
  <pre><code>    TestSupplierImpl test = new TestSupplierImpl(); // line 1
      test.model = new TestSupplierModel(3); // line 2
      Boolean resultado = test.get(); // line 3
  </code></pre>
  
  <p>Line 1: creating a new instance of TestSupplierImpl. This new instance's predicate has a null reference of <em>model</em>. This makes sense because at the moment of creation of the predicate, model reference is null.
  Line 2:  assign to variable <em>model</em> a new instance of TestSupplierModel.
  Line 3: test.predicate now has <em>model</em> reference with the new assigned value. <strong>Why is this ?</strong> </p>
  
  <p>I don't understand why ,when I changed <em>model</em> reference, the predicate updates its model reference to the new one. How is that ?</p>
  
  <p>Thanks in advance !</p>