title: lambda with non-static methods in Java 8
body: <p>I am trying to learnd lambdas in new Java 8. There is one interesting thing. If method has the same signature as functional interface, it can be assigned to it using lambdas API. For instance.</p>
  
  <pre><code>Comparator&lt;Integer&gt; myComp = Integer::compare;
  </code></pre>
  
  <p>This method(Integer.compare) is static, takes two values, everything is perfect. the signature the same as in interface method compare. BUT this can be made with non-static methods, for example</p>
  
  <pre><code>Comparator&lt;Integer&gt; myComp = Integer::compareTo.
  </code></pre>
  
  <p>This method is non-static (instance level) and furthermore, it takes only one value. As I have understood, there is no non-static methods in Java, every method is static but if it is not marked as static it takes <code>this</code> as the first parameter. As following</p>
  
  <pre><code>compareTo(this,Integer value).
  </code></pre>
  
  <p>It would be reasonable to assume that result will be undefined because of comparing object and integer. BUT THIS WORKS.</p>
  
  <pre><code>Comparator&lt;Integer&gt; comparator = Integer::compareTo;
  Comparator&lt;Integer&gt; comparator2 = Integer::compare;
  System.out.println(comparator.compare(1,2));
  System.out.println(comparator2.compare(1,2));
  </code></pre>
  
  <p>This works equally.
  I have debugged call method stack. While calling method compare of comparator object without creating instance, <code>this</code>. Value is already initialized by the first parameter and of course this is valid reference to the object. </p>
  
  <p>SO the question is How does this work? While calling method compiler checks, if the class has only one field which has the same type as first param in method, if class has compiler implictly creates new instance of the class with initialized field or how does it work?</p>