Master java skills

Method references, Double Colon (::) Operator

Method reference is used to refer abstract method of a functional interface with a concrete method of another class. The class that uses method reference doesn’t need to implement the interface. It is compact form of lambda expression. It is represented using double colon (::) operator. Lambda expressions can be replaced with method references. Let’s see one example to understand the concept.

Method reference types

Method reference can be divided in 3 types

  1. Method reference to a static method
  2. Method reference to an instance method
  3. Method reference to a Constructor

1. Method reference to a static method

syntax:

<class-name> :: <static-method-name>

Example

In the below example, we have functional interface Executable and its abstract method void execute(). We will use a static method called executeMethod() of MethodReferenceExample class to refer this method. We have to make sure that the signature of the method that will refer to void execute() method should be exactly same.

package com.javatrainingschool;

@FunctionalInterface
public interface Executable {
	
	void execute();

}
package com.javatrainingschool;

public class MethodReferenceExample {

	//we will use this static method to refer execute() method of Executable interface
	public static void executeMethod() {
		System.out.println("Executing...");
	}
	
	public static void main(String[] args) {
		
                //static method reference
                //below executeMethod() is referring to execute() method of 
                //Executable interface
		Executable executable = MethodReferenceExample :: executeMethod;
		executable.execute();
		
	}
}
Output :
Executing...

Method reference example with parameter

In the below example, we will use method reference in forEach() method. We will refer void accept(T t) method with a static method displayBookName(Book book). Since, we are using forEach() method on a list of Book, void displayBookName(Book book) method will take book as a parameter.

package com.javatrainingschool;

public class Book {

	private int id;
	private String name;
	
	//getters and setters
	//constructor
        //toString
}
   
package com.javatrainingschool;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class LambdaWithForEachExample {

	//below static method will be used as method reference
	public static void displayBookName(Book book) {
		System.out.println("Book name is : " + book.getName());
	}
	
	public static void main(String[] args) {
		
		List<Book> names = new ArrayList<>();
		names.add(new Book(1, "Master Java"));
		names.add(new Book(2, "HeadFirst JSP"));
		names.add(new Book(3, "Let Us C"));
		names.add(new Book(4, "Hibernate in One Week"));
		
		names.forEach(LambdaWithForEachExample :: displayBookName);
			
	}
}
Output :
Book name is : HeadFirst JSP
Book name is : Hibernate in One Week
Book name is : Let Us C
Book name is : Master Java

Reference to an instance method

An instance method can also be referred for the single abstract method of a functional interface.

Syntax :

<instance-variable-name> :: <instance-method-name>

Example

package com.javatrainingschool;

@FunctionalInterface
public interface Executable {
	
	void execute();

}
package com.javatrainingschool;

public class MethodReferenceExample {
	
	public void executeMethod() {
		System.out.println("Executing...");
	}
	
	public static void main(String[] args) {
		
		Executable executable = new MethodReferenceExample() :: executeMethod;
		executable.execute();
		
	}
}
Output :
Executing...

Instance method reference example to run() method of Runnable

In the below example, we will refer an instance method to the run() method of Runnable interface

package com.javatrainingschool;

public class MethodReferenceExample {
	
	public void runMethod() {
		System.out.println("Thread running...");
	}
	
	public static void main(String[] args) {
		
		Runnable r = new MethodReferenceExample() :: runMethod;
		Thread t1 = new Thread(r);
		t1.start();
		
	}
}
Output :
Thread running...

Reference to a constructor

A functional interface’s abstract method can also be referred by a constructor of a class.

Syntax :

<class-name> :: new

Example :

Let’s take Runnable example here to refer its run() method.

package com.javatrainingschool;

public class MethodReferenceExample {
	
        //no-arg constructor
	public MethodReferenceExample() {
		System.out.println("MethodReferenceExample object initialized.");
	}
	
	public static void main(String[] args) {
		
		Runnable r = MethodReferenceExample :: new;
		Thread t1 = new Thread(r);
		t1.start();
		
	}
}
Output :
MethodReferenceExample object initialized.