Table of Contents (Click on links below to navigate)
- 1 What is Method Reference(::) ?
- 2 Syntax to write Method References
- 3 Examples of each Type
- 4 Example of Reference to an Instance Method of an Arbitrary Object of a Particular Type
- 5 Reference to super class & child class method using super & this keyword
What is Method Reference(::) ?
As we have seen in Lambda expression’s topic that we use lambda expressions to implement Functional interfaces with minimum lines of code and even to get better code readability. Similarly, we can use Method References(::) Java 8 to implement Functional interfaces with even lesser code again than lambda expressions and this time we get the benefit of code re-usability as well, because we don’t provide implementation for functional interface. Instead, we provide reference to already existing method(with similar argument types) to simplify the implementation of the functional interface using double colon (::) operator. This process of providing reference to pre-existing method is called Method reference.
From the above definition it is clear that while applying Method References our focus is always on the pre-existing methods which are well suited to the implementation of Functional Interface. In fact, pre-existing methods should have same arguments in number & type and there are no restrictions on their return type as such. Of course, here one thing is equally important to keep in mind that Method references & Lambda expressions both can only be used in context of Functional Interfaces.
In the above code snippet, Interface A, is a functional interface. The Single abstract method(SAM), getName(String name) of interface A has the same argument in number & type as the pre-existing method getInfo(String info) of class Test. Therefore, we can refer getInfo(String info) as an implementation of Functional interface A without any problem. Hence, the syntax for Method reference will be ClassName::methodName if it is static method.
Syntax to write Method References
First, we will categorize the method references before knowing how to write them. Here, syntax to write Method Reference(::) in Java 8 is different in different cases. In general, we can provide Method references in three ways, sometimes called the types of it.
- Static Method References
- Instance method/non-static Method References
- Constructor References
Now, let’s try to observe them one by one from below table.
Note : In addition, we have two more besides above three:
- Method References of an arbitrary object of a given type
- Method References from a super class method. We will cover them separately in below sections.
Examples of each Type
Furthermore, in this section we will see the example of each type one by one.
Method reference to a static method of a class :
In the above example, we have functional Interface A. We have implemented the single abstract method checkSingleDigit(int x) using Lambda expression in highlighted line. But while using Method reference, we have just referred the similar already existing static method isSingleDigit(int x) of class Digit.
Method reference to an Instance method of a class :
In the above example, we have functional Interface B. We have implemented the single abstract method add(int x, int y) using Lambda expression in highlighted line. While using Method reference, we have just referred the similar already existing method sum(int a, int b) of class Addition but after creating an object of the class this time.
When single abstract method’s return type is any Object, we will go with the constructor reference.
In the above code snippet, we have two Functional interfaces C & D. Interface C has abstract method with no arguments whereas D has with arguments. We will have same constructor reference code in both cases as shown in the highlighted lines of code. Also, if you run the code in your development environment, you will get the same output in each case.
Example of Reference to an Instance Method of an Arbitrary Object of a Particular Type
This type of Instance methods refers to a non-static method that are not bound to a receiver object. In this case we don’t need to create an object of a particular type.
In the above code snippet we have shown the three ways to implement a Functional Interface (Comparator) simultaneously. Of course, All of them outputs the same result.
Difference between reference to an Instance method of a particular object & an arbitrary Object of a given type
You might have some doubt on difference between both references, so now it’s right time to talk about them. To illustrate, let’s observe the examples given in the table below. I am sure you will get the clear idea on differences. There is no need to explain more about this.
Reference to super class & child class method using super & this keyword
Now we will see the use of super & this keyword in writing Method references accordingly.
As in the above code snippet, we have a functional interface ‘A‘ with Single Abstract method sayHello(). We have two classes SuperClass & ChildClass. Existing methods superHello() & childHello() are referred to write method references using super & this keyword respectively.
Furthermore, If you want to learn more about Method Reference(::) Java 8, kindly visit official Oracle Documentation.