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:
- Static Method Reference.
- Instance Method Reference.
- Reference to a Method of Arbitrary Instance.
- 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.
- public class mr_eg
- {
- public static void square()
- {
- for(int i = 1;i < 11; i++)
- {
- System.out.println("square of "+i+" is " + (i*i));
- }
- }
- public static void main(String[] args)
- {
- System.out.println("");
- new Thread(mr_eg :: square).start();
- }
- }
Output: Note that "Use of double colon (::)" is for reference. We are not invoking the method.
Working with Instance method reference
Example
- public class mr_eg1
- {
- public void table()
- {
- System.out.println("Table of 2 is:");
- for(int i = 1;i < 11; i++)
- {
- int a=2;
- System.out.println((i*a));
- }
- }
- public static void main(String[] args)
- {
- System.out.println("");
- mr_eg1 obj=new mr_eg1();
- new Thread(obj :: table).start();
- }
- }
Output
Working with Reference to a method of arbitrary instance
Example
- import java.io.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.function.Supplier;
- public class mr_eg2
- {
- public String low(String s)
- {
- return s.toUpperCase();
- }
- public void funcStr(Function<String, String> stringOperator, String s)
- {
- System.out.println(stringOperator.apply(s));
- }
- public static void main(String args[])
- {
- mr_eg2 obj=new mr_eg2();
- obj.funcStr(String::toUpperCase, "this string prints in upper case");
- }
- }
Output
Working with Constructor reference
Example
- class Emp
- {
- String name;
- Integer age;
- Emp(String name, Integer age)
- {
- this.name = name;
- this.age = age;
- }
- }
- @FunctionalInterface
- interface EmpProvider
- {
- Emp getEmp(String name, int age);
- }
- public class mr_eg3
- {
- public static void main(String[] args)
- {
- EmpProvider provider = Emp::new;
- Emp emp = provider.getEmp("Anas", 24);
- System.out.printf("Name: %s%n", emp.name);
- System.out.printf("Age: %d%n", emp.age);
- }
- }
Output
Example: This example implements all of the preceding Method References.
- import java.io.*;
- import java.util.*;
- import java.util.function.*;
- import java.util.function.Supplier;
- public class mr_eg4
- {
- public int add(int x, int y)
- {
- return x + y;
- }
- public static int mul(int x, int y)
- {
- return x * y;
- }
- public String low(String s)
- {
- return s.toLowerCase();
- }
- public void funcInt(IntBinaryOperator operator, int x, int y)
- {
- System.out.println(operator.applyAsInt(x, y));
- }
- public void funcStr(Function<String, String> stringOperator, String s)
- {
- System.out.println(stringOperator.apply(s));
- }
- public mr_eg4()
- {
- System.out.println("This Line is print by using Constructor reference");
- }
- interface int_mr
- {
- mr_eg4 getmr_eg4();
- }
- public static void main(String args[])
- {
- System.out.println("");
- mr_eg4 obj=new mr_eg4();
- obj.funcInt(obj::add, 7, 9);
- obj.funcInt(mr_eg4::mul, 5, 6);
- obj.funcStr(String::toLowerCase, "THIS STRING PRINTS IN LOWER CASE");
- int_mr obj1=mr_eg4::new;
- mr_eg4 obj2=obj1.getmr_eg4();
- }
- }
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.