Signup/Sign In
LAST UPDATED: JUNE 30, 2020

Java 8 Method Reference

Method reference is a new concept in Java which was added in Java 8 version. This feature allows us to call a method by using method reference. Its primary uses are to call the method of functional interface and use '::' operator to separate the class or object from the method name. For example, className::methodName or object::methodName. We will see it later in our examples.

We hope you are aware of the Functional Interface because this topic is co-related to it. If you are a beginner then we recommend you to first read our post Java Functional Interface and then move on further to this post.

Types of Method Reference in Java

There are four types of a method reference in Java, see the following:

  • Reference to a static method

  • Reference to an instance method

  • Reference to a constructor

Static Method Reference

To refer to a static method, there is the following syntax that refers to a static method of another class or interface.

Syntax

className::staticMethod

Example:

In this example, we are referring a static method using a method reference concept.

interface JavaInterface{
	void showInterfaceInfo();
}

public class STDemo {
	static void show() {
		System.out.println("This is called using method reference");
	}
	public static void main(String[] args) {
		// Referring Method
		JavaInterface ji = STDemo::show;
		ji.showInterfaceInfo();
	}
}

This is called using method reference

Example: Referring library static method

In this example, we are referring a static method of Math class. The static method we are referring to a user-defined or library method.

interface JavaInterface{
	int findMax(int a, int b);
}

public class STDemo {
	
	public static void main(String[] args) {
		// Referring Method
		JavaInterface ji = Math::max;
		int result = ji.findMax(10, 20);
		System.out.println(result);
		
	}
}

20

Instance Method Reference

When we refer an instance method using the method reference concept then it is called instance method reference. We use a class object to refer to the instance method. The syntax of instance method reference is given below.

Syntax

object::instanceMethod

Example:

In this example, we are referring a method show() to showInterfaceInfo() method. Calling of showInterfaceInfo() is actually refer and call show() method.

interface JavaInterface{
	void showInterfaceInfo();
}

public class STDemo {
	void show() {
		System.out.println("This is called using instance method reference");
	}
	public static void main(String[] args) {
		STDemo demo = new STDemo();
		// Referring Method
		JavaInterface ji = demo::show;
		ji.showInterfaceInfo();
	}
}

This is called using method reference

Example: Referring Library Instance Method

Here, we are referring length(), an instance method of String class from the stringLenght() method of JavaInterface. The length() method is a built-in method of String that we are referring to here by our own method.

interface JavaInterface{
	int stringLength();
}

public class STDemo {
	public static void main(String[] args) {
		STDemo demo = new STDemo();
		// Referring Method
		JavaInterface ji = "hello"::length;
		int result = ji.stringLength();
		System.out.println(result);
	}
}

5

Reference To a Constructor

This is one of the types of method reference that allows us to call a constructor with a little different syntax.

Syntax

className::new

Example:

In this example, we are referring a constructor from a method reference. A constructor can be a default or parameterized and able to be called by method reference.

interface JavaInterface{
	void showInfo();
}

public class STDemo {
	
	STDemo(){
		System.out.println("Constructor using method reference");
	}
	
	public static void main(String[] args) {
		// Referring Constructor
		JavaInterface a = STDemo::new;
		a.showInfo();
		
	}
}


Constructor using method reference

Another Example: Calling a Parameterized Constructor

interface JavaInterface{
	void showInfo(String msg);
}

public class STDemo {
	
	STDemo(String msg){
		System.out.println(msg);
	}
	
	public static void main(String[] args) {
		// Referring Constructor
		JavaInterface a = STDemo::new;
		a.showInfo("StudyTonight");
		
	}
}


StudyTonight



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.