I was just learning about method refernce concept of java 8.What I found strange is the example where a method reference is assigned to the interface variable without implementing the interface, And calling abstract method of interface is calling the method referenced.
interface Sayable {
void say();
}
public class InstanceMethodReference {
public void saySomething() {
System.out.println("Hello, this is non-static method.");
}
public static void main(String[] args) {
InstanceMethodReference methodReference = new InstanceMethodReference();
Sayable sayable = methodReference::saySomething;
sayable.say();
}
}
Above code prints the message of saySomething method, I am trying to understand how the memory allocation for methods and objects is done here and how overall this works.
Any help appreciated.
This is just a syntactic sugar to an anonymous implementation or a lambda with a closure (referring to a state outside of the definition, in your case the instance of methodReference
). Method references and lambdas are treated equally with that respect. So the same memory allocation really.
The use of method references (or lambdas) is possible when an interface has only one non-static method and the signature of that method matches that of the lambda or the method reference. In this case the compiler will know how to wrap it and therefore it will be assignable. It does not matter if it is one of "standard" interfaces such as Function
, Consumer
or Supplier
or a custom one, it should be a functional interface that's all.
The following is quoted from the official Oracle Java documentation:
Arrays.sort(rosterAsArray, Person::compareByAge);
The method reference Person::compareByAge
is semantically the same as the lambda expression (a, b) -> Person.compareByAge(a, b)
. Each has the following characteristics:
While your Sayable
does not explicitly declare that it is a @FunctionalInterface
, it indeed is one, as it has exactly one non-static method.
Any functional interface can be assigned a lambda-like expression (such as () -> printf("bla bla")
) or a method reference (such as methodReference::saySomething
). That's it.
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