Java 8 – Method References

Method References make it clear when referring to the existing method by name.

In the article Lambda Expressions and Functional Interfaces, we use lambda expressions to create anonymous methods and sometimes, calling an existing method is the only thing it does.

When using lambda expression without input arguments or those input arguments doesn’t make sense, we can use Method References to simplify the syntax.

This tutorial introduces four kinds of Method References and how to use them efficiently with some Functional Interfaces.

I. Method References

Method reference points to the method by its name by replacing a single-method lambda expression.
The syntax of a Method reference is: Object/Class/Type :: methodName

For example, the lambda expression:

can be replaced with the method reference:

But we should know that it can’t be used for any method. To use a method reference, we need a lambda expression as a method argument which can be used when having a functional interface (an interface with just one abstract method).

II. Kinds of Method References

There are four kinds of method references:
– Reference to a static method: ContainingClass::staticMethodName
– Reference to an instance method of a particular object: containingObject::instanceMethodName
– Reference to an instance method of an arbitrary object of a particular type: ContainingType::instanceMethodName
– Reference to a constructor: ClassName::new

1. Reference to a static method

We have a static method addition in MathOperation class:

To use lambda expression, in this case, we should have a function with functional interface ToIntBiFunction as input argument:

And the lambda expression will be used:

Instead, Method Reference makes it lean:

You can see that we don’t need to add argument (x,y) or Arrow Token (->) to the statement, syntax only requires ContainingClass and staticMethodName with double colon symbol (::) between.
ContainingClass::staticMethodName

2. Reference to an instance method of a particular object

In this kind, we will replace a lambda expression:
(arguments) -> containingObject.instanceMethodName(arguments)
with method reference:
containingObject::instanceMethodName
We have a method getOperationName in MathOperation class:

A function with functional interface Function as input argument:

The lambda expression and Method Reference will be used:

3. Reference to an instance method of an arbitrary object of a particular type

we will replace a lambda expression:
(object) -> object.instanceMethodName()
with method reference:
ContainingTypeofObject::instanceMethodName
We have a method getOperationNameNew in MathOperation class.
The purpose is using the instance object of MathOperation with its property [String operation]:

A function with functional interface Function as input argument:

The lambda expression and Method Reference will be used:

4. Reference to a constructor

we will replace a lambda expression:
(arguments) -> new ClassName(arguments)
with method reference:
ClassName::new
We have 2 constructor methods in MathOperation class:

Two Functions with functional interfaces Supplier and Function as input arguments to create new MathOperation in two ways (with and without input argument):

The lambda expression and Method Reference will be used:

III. Source code

Technology:
– Java 8
– Eclipse Mars.1 Release (4.5.1)
methodreference
Run the source code above, we can see the results in Console Window:


Related Posts



Got Something To Say:

Your email address will not be published. Required fields are marked *

*