title: Invalid constructor reference when using local class?
body: <p>Given the following code:</p>
  
  <pre><code>package com.gmail.oksandum.test;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Test {
  
      public static void main(String[] args) {
      }
  
      public void foo() {
          class LocalFoo {
  
              LocalFoo(String in) {
                  //Some logic
              }
  
          }
  
          List&lt;String&gt; ls = new ArrayList&lt;&gt;();
          ls.stream().map(LocalFoo::new); //Line 21
      }
  
  }
  </code></pre>
  
  <p>my IDE gives me no errors. That is, until I try to build the project and run it. When I do that it gives me a compiler error that looks like this:</p>
  
  <pre><code>Error:(21, 24) java: incompatible types: cannot infer type-variable(s) R
      (argument mismatch; invalid constructor reference
        cannot access constructor LocalFoo(java.lang.String)
          an enclosing instance of type com.gmail.oksandum.test.Test is not in scope)
  </code></pre>
  
  <p>Now, I figured, given the error message, that this wouldn't happen if foo() were static. And quite right, this only happens if foo() is an instance method. And it only happens if LocalFoo is a local class in the instance method, and only if a constructor reference is used (i.e never a regular method reference).</p>
  
  <p>What's more, if I change line 21 into</p>
  
  <pre><code>ls.stream().map(str -&gt; new LocalFoo(str));
  </code></pre>
  
  <p>the compiler suddenly gives no error.</p>
  
  <p>So to recap. If I try to use a constructor reference on a local class declared within an instance method, the compiler complains about not being able to access the constructor, about which I am confused.</p>
  
  <p>If someone could shed some light on why this happens it would be appreciated.
  Thanks.</p>