Constructor reference and Method reference in Java 8

In this article, we’ll be discussing constructor reference and method reference introduced in Java 8 with the help of code example.

Introduction

An alternative to Lambda expression is Method reference and constructor reference
– Both Method reference and Constructor reference allow us to reuse the existing code
Double colon operator is used to make reference
– They provide code reusability
– They are used to map a method or constructor with corresponding single abstract method of a given functional interface
– Number of method arguments must be same as functional interface method
– But different return types are allowed

Method Reference

Method reference is another way of implementing functional programming in Java. Instead of creating anonymous method or Lambda expression, one can refer to existing method using that specific method name .

In simple words, it is a way of referencing an existing instance or static method  with corresponding abstract method of functional interface. So that, it can be viewed as an instance of functional interface.

Syntax :

For instance method :

 ObjetName :: MethodName

Let’s see a simple Java program to understand the working of Method Reference in case of instance method.

@FunctionalInterface // Not Compulsory
interface ExampleInterface{
void print();
}

public class MethodReferenceExample {

void show(){
System.out.println("Body of show() would be used as print body");
}

public static void main(String[] args) {
MethodReferenceExample obj = new MethodReferenceExample();
ExampleInterface methodReference = obj ::show;
methodReference.print();
}
}

 

Output:

Body of show() would be used as print body

Explanation:

For referencing instance method, double colon operator is used, followed by object and lead by method name.

For Static Method

- ClassName: MethodName

Let’s see another simple Java code to understand the working of Method Reference in case of static Method.

@FunctionalInterface // Not Compulsory
interface ExampleInterface{
void print();
}

public class MethodReferenceExample {

static void show(){
System.out.println("Body of show() would be used as print body: static method");
}

public static void main(String[] args) {
ExampleInterface methodReference = MethodReferenceExample::show;
methodReference.print();
}
}

Output:

Body of show() would be used as print body: static method

Explanation:

For referencing static method, double colon operator is used, followed by class name and lead by method name.

Constructor Reference

Constructor reference does the same thing but in different way, instead of referencing method, it maps the functional interface method with constructor of a particular class. It uses ‘new’ keyword to target a desirable constructor.

Let’s see another simple Java code to understand the working of Constructor Reference

@FunctionalInterface // Not Compulsory
interface ExampleInterface{
void print();
}

public class MethodReferenceExample {
MethodReferenceExample() {
System.out.println("This is a simple example constructor reference");
}

public static void main(String[] args) {
ExampleInterface methodReference = MethodReferenceExample ::new;
methodReference.print();
}
}

Output:

This is a simple example constructor reference

Explanation:

For referencing constructor, double colon operator is used, followed by class name and lead by new keyword.

Leave a Reply