title: Java 8 Comparator with Method reference
body: <p>Trying to understand Method references and am having a hard time around it.
  Stuck with the comparator example.</p>
  <p>To make things easier for my brain I keep starting from the basics ( anonymous class implementation &gt;&gt; lamba expression &gt;&gt; method reference )
  It is with method references and Comparator example that I am not able to get it to work.</p>
  <p>POJO that I want to sort ( picked up from some internet example - mkyong)</p>
  <pre><code>public class Fruit{
     private int quantity;
     private String name;
     ....
  }
  </code></pre>
  <p>Creating test data to play with:</p>
  <pre><code>Fruit[] fruits = new Fruit[3];
  Fruit pineapple = new Fruit(&quot;pineapple&quot;,10);
  Fruit orange= new Fruit(&quot;orange&quot;,4);
  Fruit kiwi= new Fruit(&quot;kiwi&quot;,15);
  
  fruits[0] = pineapple;
  fruits[1] = orange;
  fruits[2] = kiwi;
  </code></pre>
  <p>The Anonymous sorting class implementation:</p>
  <pre><code>Comparator&lt;Fruit&gt; anonymousSortByname = new Comparator&lt;Fruit&gt;(){
       public int compare(Fruit f1,Fruit f2){
          return f1.getName().compareTo(f2.getName());
       }
  };
  </code></pre>
  <p>To sort :</p>
  <pre><code>Arrays.sort(fruits,anonymousSortByname);
  </code></pre>
  <p>Now that this works I have understood that anonymous classes can be converted into lambda expressions ( provided they are functional interfaces etc ... )</p>
  <p>The Lambda implementation:</p>
  <pre><code>Comparator&lt;Fruit&gt; lambdaSortByname = 
   (Fruit f1,Fruit f2) -&gt; f1.getName().compareTo(f2.getName());
  </code></pre>
  <p>Ok so things are making sense upto this point.</p>
  <p>My understanding: &quot;wherever lambdas are used they <strong>could</strong> be replaced with 'Method references'&quot;</p>
  <p>Did read on SO and over the internet that it is not mandatory to use 'Method references' but my goal is to understand them - so whether to use or not is not my question :)</p>
  <p>Also was reading here:
  <a href="https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx" rel="nofollow noreferrer">method reference1</a>  and <a href="https://www.baeldung.com/java-method-references" rel="nofollow noreferrer">here</a> and <a href="https://www.baeldung.com/java-8-double-colon-operator" rel="nofollow noreferrer">here</a> .
  However I am having a difficult time with Method reference specifically when I am trying to implement it with Comparator.</p>
  <p>Here is what I have tried and it gives me a compilation error.</p>
  <pre><code>Comparator&lt;Fruit&gt; methodRefComparator = Comparator::compare;
  </code></pre>
  <p>Error on compilation:</p>
  <blockquote>
  <p>Cannot make a static reference to non static block of code</p>
  </blockquote>
  <p>In the links the example does talk about a method 'comparing' in Comparator interface but gets fuzzy at this point....</p>
  <p><strong>Edit#1</strong></p>
  <p>Based on comments below have revised the question:</p>
  <p>The confusion is around how '<strong>comparing</strong>' method of Comparator works.</p>
  <p>I did get a working example ( by googling and not by my own understanding) :</p>
  <p>First created a function that accepts a 'Fruit' as input and returns a String.</p>
  <pre><code>Function&lt;Fruit, String&gt; funcName = (Fruit f1) -&gt; f1.getName();
  </code></pre>
  <p>Then passed this 'Function' to the 'comparing' method of 'Comparator' to get back the desired comparator.</p>
  <pre><code>Comparator&lt;Fruit&gt; sortByName = Comparator.comparing(funcName);
  </code></pre>
  <p>Now the sorting works:</p>
  <pre><code>Arrays.sort(fruits,sortByName );
  </code></pre>
  <p>but .. am unable to get my head around how does it work .
  I have not provided the implementation of the Comparator ( f1.getName() - f2.getName()) so how did it work ?</p>
  <p>Is this more like a contract where the compiler is able to understand that since I have provided it a function with the implementation of 'getName' it would use it in the returned Comparator ?</p>