I am trying to use Java 8 method references in my code. There are four types of method references available.
With Static method reference
and Constructor reference
i have no problem, but Instance Method (Bound receiver)
and Instance Method (UnBound receiver)
really confused me. In Bound
receiver, we are using an Object reference variable for calling a method like:
objectRef::Instance Method
In UnBound
receiver we are using Class name for calling a method like:
ClassName::Instance Method.
I have the following question:
Bound
and Unbound
receiver method references?Bound
receiver and where should we use Unbound
receiver?I also found the explanation of Bound
and Unbound
receiver from Java 8 language features books, but was still confused with the actual concept.
In unbound references, the receiving object is specified when the function object is applied, via an additional parameter before the method's declared parameters. Unbound references are often used as mapping and filter functions in stream pipelines.
Java provides a new feature called method reference in Java 8. Method reference is used to refer method of functional interface. It is compact and easy form of lambda expression. Each time when you are using lambda expression to just referring a method, you can replace your lambda expression with method reference.
A static method reference refers to a static method in a specific class. Its syntax is className::staticMethodName , where className identifies the class and staticMethodName identifies the static method. An example is Integer::bitCount .
How to print the values using method reference? in method references you just give the name of the function (println), they don't take arguments. You can create your own function that accepts a string and calls toUpperCase and then println, and then give the name of your function as the method reference name.
The idea of the unBound receiver such as String::length
is you're referring to a
method of an object that will be supplied as one of the lambda's parameters. For example,
the lambda expression (String s) -> s.toUpperCase()
can be rewritten as String::toUpperCase
.
But Bounded refers to a situation when you’re calling a method in a
lambda to an external object that already exists. For example, the lambda expression () -> expensiveTransaction.getValue()
can be rewritten as expensiveTransaction::getValue
.
Situations for three different ways of method reference
(args) -> ClassName.staticMethod(args)
can be ClassName::staticMethod
// This is static (you can think as unBound also)
(arg0, rest) -> arg0.instanceMethod(rest)
can be ClassName::instanceMethod
(arg0
is of type ClassName
) // This is unBound
(args) -> expr.instanceMethod(args)
can be expr::instanceMethod
// This is Bound
Answer retrieved from Java 8 in Action book
Basically, unbound receivers allow you to use instance methods as if they were static methods with a first parameter of the declaring type - so you can use them as functions by passing in whatever instance you want. With a bound receiver, the "target" instance is effectively part of the function.
An example might make this clearer:
import java.util.function.*;
public class Test {
private final String name;
public Test(String name) {
this.name = name;
}
public static void main(String[] args) {
Test t1 = new Test("t1");
Test t2 = new Test("t2");
Supplier<String> supplier = t2::method;
Function<Test, String> function = Test::method;
// No need to say which instance to call it on -
// the supplier is bound to t2
System.out.println(supplier.get());
// The function is unbound, so you need to specify
// which instance to call it on
System.out.println(function.apply(t1));
System.out.println(function.apply(t2));
}
public String method() {
return name;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With