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 it calls 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 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. {   
  3.     public static void square() //static method  
  4.     {  
  5.         for(int i = 1;i < 11; i++)   
  6.         {  
  7.             System.out.println("square of "+i+" is " + (i*i));  
  8.         }  
  9.     }  
  10.     public static void main(String[] args)   
  11.     {  
  12.         System.out.println("");  
  13.         new Thread(mr_eg :: square).start(); //ref. of static method  
  14.     }  
  15. }  

 Output: Note that "Use of double colon (::)" is for reference. We are not invoking the method.


Working with Instance method reference

Example

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

 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. {  
  7.     public String low(String s)  
  8.     {  
  9.         return s.toUpperCase();  
  10.     }  
  11.     public void funcStr(Function<String, String> stringOperator, String s)   
  12.     {  
  13.         System.out.println(stringOperator.apply(s));  
  14.     }  
  15.     public static void main(String args[])  
  16.     {  
  17.         mr_eg2 obj=new mr_eg2();  
  18.         obj.funcStr(String::toUpperCase, "this string prints in upper case");  
  19.     }  
  20. }  

 Output


Working with Constructor reference

Example

  1. class Emp  
  2. {  
  3.    String name;  
  4.    Integer age;  
  5.    Emp(String name, Integer age)//parameterised constructor  
  6.    {  
  7.       this.name = name;  
  8.       this.age = age;  
  9.    }  
  10. }  
  11. @FunctionalInterface  
  12. interface EmpProvider  
  13. {  
  14.    Emp getEmp(String name, int age);  
  15. }  
  16. public class mr_eg3  
  17. {  
  18.    public static void main(String[] args)  
  19.    {  
  20.       EmpProvider provider = Emp::new;  
  21.       Emp emp = provider.getEmp("Anas"24);  
  22.       System.out.printf("Name: %s%n", emp.name);  
  23.       System.out.printf("Age: %d%n", emp.age);  
  24.    }  
  25. }  

 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. {  
  7.     public int add(int x, int y)   
  8.     {  
  9.         return x + y;  
  10.     }  
  11.     public static int mul(int x, int y)   
  12.     {  
  13.         return x * y;  
  14.     }  
  15.     public String low(String s)   
  16.     {  
  17.         return s.toLowerCase();  
  18.     }  
  19.     public void funcInt(IntBinaryOperator operator, int x, int y)   
  20.     {  
  21.         System.out.println(operator.applyAsInt(x, y));  
  22.     }  
  23.     public void funcStr(Function<String, String> stringOperator, String s)   
  24.     {  
  25.         System.out.println(stringOperator.apply(s));  
  26.     }  
  27.     public mr_eg4()  
  28.     {  
  29.         System.out.println("This Line is print by using Constructor reference");  
  30.     }  
  31.     interface int_mr  
  32.     {  
  33.         mr_eg4 getmr_eg4();  
  34.     }  
  35.     public static void main(String args[])  
  36.     {  
  37.         System.out.println("");  
  38.         mr_eg4 obj=new mr_eg4();  
  39.         obj.funcInt(obj::add, 79);//object  
  40.         obj.funcInt(mr_eg4::mul, 56);//class  
  41.         obj.funcStr(String::toLowerCase, "THIS STRING PRINTS IN LOWER CASE");  
  42.         int_mr obj1=mr_eg4::new;  
  43.         mr_eg4 obj2=obj1.getmr_eg4();  
  44.     }  
  45. }    

 Output


Summary

The summary of this article is that Method Reference is one of the very useful features of Java 8. By using Method References, the code will become more readable that is good in long coading.

Up Next
    Ebook Download
    View all
    Learn
    View all