title: Java "The blank final field may not have been initialized" Anonymous Interface vs Lambda Expression
body: <p>I've recently been encountering the error message "The blank final field obj may not have been initialized".
  <p>
  Usually this is the case if you try to refer to a field that is possibly not assigned to a value yet. Example class:</p>
  
  <pre><code>public class Foo {
      private final Object obj;
      public Foo() {
          obj.toString(); // error           (1)
          obj = new Object();
          obj.toString(); // just fine       (2)
      }
  }
  </code></pre>
  
  <p>I use Eclipse. In the line <code>(1)</code> I get the error, in the line <code>(2)</code> everything works. <strong>So far that makes sense.</strong>
  <p>
  Next I try to access <code>obj</code> within an anonymous interface I create inside the constructor.</p>
  
  <pre><code>public class Foo {
      private Object obj;
      public Foo() {
          Runnable run = new Runnable() {
              public void run() {
                  obj.toString(); // works fine
              }
          };
          obj = new Object();
          obj.toString(); // works too
      }
  }
  </code></pre>
  
  <p>This works, too, since I do not access <code>obj</code> in the moment I create the interface. I could also pass my instance to somewhere else, then initialize the object <code>obj</code> and then run my interface. (However it would be appropriate to check for <code>null</code> before using it). <strong>Still makes sense.</strong>
  <p>
  <strong>But now I shorten the creation of my <code>Runnable</code> instance to</strong> the burger-arrow version by using <strong>a lambda expression</strong>:</p>
  
  <pre><code>public class Foo {
      private final Object obj;
      public Foo() {
          Runnable run = () -&gt; {
              obj.toString(); // error
          };
          obj = new Object();
          obj.toString(); // works again
      }
  }
  </code></pre>
  
  <p>And here is where I can't follow anymore. Here I get the warning again. I am aware that the compiler doesn't handle lambda expressions as usual initializations, it doesn't "replace it by the long version". However, why does this affect the fact that I do not run the code part in my <code>run()</code> method at creation time of the <code>Runnable</code> object? I am still able to do the initialization <em>before</em> I invoke <code>run()</code>. So technically it is possible not to encounter a <code>NullPointerException</code> here. (Though it would be better to check for <code>null</code> here, too. But this convention is another topic.)
  <p>
  What is the mistake I make? What is handled so differently about lambda that it influences my object usage the way it does?
  <p>
  I thank you for any further explanations.</p>