Method Reference in Java 8

Introduction
  

In this article, we learn about Method Reference, a new feature of Java 8.  Method Reference provides a way to use a method, without executing it.  For instance, if we already have a declared method, we can call it using a method reference . After reading, you will be able to implement a method reference.
 

Method Reference

 
You are already familiar with lambda, expressions that are nothing, but can call an existing method.  Method reference is an important feature of Java 8, related to Lambda Expressions.  Since, I said earlier, it provides a way to use a method without invoking them.  For already defined methods, we have a double colon (: :) operators for implementation.  The method of passing a method reference and functional interface, should be matched for return type and arguments.  It could be implemented for both a Static method and a Class method.
There are four types of Method References:
  1. Static Method Reference.
  2. Instance Method Reference.
  3. Reference to a Method of Arbitrary Instance.
  4. Constructor Reference.
Type Syntax
Static method reference ContainingClass :: staticMethodName
Instance method reference ContainingObject :: instanceMethodName
Reference to a method of arbitrary instance ContainingType :: methodName
Constructor reference ClassName :: new

 
Working with Static method references

 
Example: In this example we are using "static method reference". Here "square()" is the static method.
  1. public class mr_eg {  
  2.  public static void square() {  
  3.   for (int i = 1; i < 11; i++) {  
  4.    System.out.println("square of " + i + " is " + (i * i));  
  5.   }  
  6.  }  
  7.  public static void main(String[] args) {  
  8.   System.out.println("");  
  9.   new Thread(mr_eg::square).start();  
  10.  }  
  11. }   
 Output: Note that "Use of a double colon (::)" is for reference.  We are not invoking the method.
 

Working with Instance method reference

 
Example
  1. public class mr_eg1 {  
  2.  public void table() {  
  3.   System.out.println("Table of 2 is:");  
  4.   for (int i = 1; i < 11; i++) {  
  5.    int a = 2;  
  6.    System.out.println((i * a));  
  7.   }  
  8.  }  
  9.  public static void main(String[] args) {  
  10.   System.out.println("");  
  11.   mr_eg1 obj = new mr_eg1();  
  12.   new Thread(obj::table).start();  
  13.  }  
  14. }  
 Output
 

Working with Reference to a method of arbitrary instance

 
Example
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.util.function.*;  
  4. import java.util.function.Supplier;  
  5. public class mr_eg2 {  
  6.  public String low(String s) {  
  7.   return s.toUpperCase();  
  8.  }  
  9.  public void funcStr(Function < String, String > stringOperator, String s) {  
  10.   System.out.println(stringOperator.apply(s));  
  11.  }  
  12.  public static void main(String args[]) {  
  13.   mr_eg2 obj = new mr_eg2();  
  14.   obj.funcStr(String::toUpperCase, "this string prints in upper case");  
  15.  }  
  16. }   
 Output
 

Working with Constructor reference

 
Example
  1. class Emp {  
  2.  String name;  
  3.  Integer age;  
  4.  Emp(String name, Integer age) {  
  5.   this.name = name;  
  6.   this.age = age;  
  7.  }  
  8. }  
  9. @FunctionalInterface  
  10. interface EmpProvider {  
  11.  Emp getEmp(String name, int age);  
  12. }  
  13. public class mr_eg3 {  
  14.  public static void main(String[] args) {  
  15.   EmpProvider provider = Emp::new;  
  16.   Emp emp = provider.getEmp("Anas"24);  
  17.   System.out.printf("Name: %s%n", emp.name);  
  18.   System.out.printf("Age: %d%n", emp.age);  
  19.  }  
  20. }   
 Output
 
Example: This example implements all of the preceding Method References.
  1. import java.io.*;  
  2. import java.util.*;  
  3. import java.util.function.*;  
  4. import java.util.function.Supplier;  
  5. public class mr_eg4 {  
  6.  public int add(int x, int y) {  
  7.   return x + y;  
  8.  }  
  9.  public static int mul(int x, int y) {  
  10.   return x * y;  
  11.  }  
  12.  public String low(String s) {  
  13.   return s.toLowerCase();  
  14.  }  
  15.  public void funcInt(IntBinaryOperator operator, int x, int y) {  
  16.   System.out.println(operator.applyAsInt(x, y));  
  17.  }  
  18.  public void funcStr(Function < String, String > stringOperator, String s) {  
  19.   System.out.println(stringOperator.apply(s));  
  20.  }  
  21.  public mr_eg4() {  
  22.   System.out.println("This Line is print by using Constructor reference");  
  23.  }  
  24.  interface int_mr {  
  25.   mr_eg4 getmr_eg4();  
  26.  }  
  27.  public static void main(String args[]) {  
  28.   System.out.println("");  
  29.   mr_eg4 obj = new mr_eg4();  
  30.   obj.funcInt(obj::add, 79);  
  31.   obj.funcInt(mr_eg4::mul, 56);  
  32.   obj.funcStr(String::toLowerCase, "THIS STRING PRINTS IN LOWER CASE");  
  33.   int_mr obj1 = mr_eg4::new;  
  34.   mr_eg4 obj2 = obj1.getmr_eg4();  
  35.  }  
  36. }   
 Output
 

Summary

 
The summary of this article, is that Method Reference is a very useful feature of Java 8.  By using Method References, the code will become more readable, which is good in long coding.