I have the code below that does produce expected result. Could somebody explain what's happening behind the curtains? I don't understand how does the compiler/JVM knows that it's needed to invoke store(String str) on the Storer object or how does it define the doSomething(Storer s, String str) implementation.
Storer.java
public class Storer {
private List<String> storer = new ArrayList<>();
public void store(String str) {
storer.add(str);
}
@Override
public String toString() {
return "Storer [storer=" + storer + "]";
}
}
MyInterface.java
@FunctionalInterface
public interface MyInterface {
public abstract void doSomething(Storer s, String str);
}
Executor.java
public class Executor {
public void doStore(Storer storer, String s, MyInterface p) {
p.doSomething(storer, s);
}
}
TestFunctionalInterfaces.java
public class TestFunctionalInterfaces {
public static void main(String[] args) {
Storer storer = new Storer();
Executor test = new Executor();
test.doStore(storer, "I've got added", Storer::store);
System.out.println(storer);
}
}
An output is:
Storer [storer=[I've got added]]
Thanks in advance.
The method reference Store::storer
is equivalent to the lambda (s, str) -> s.store(str)
. In general, given a functional interface that expects args a1, a2, a3, ..., a method reference of this type is equivalent to a lambda that calls a1.namedMethod(a2, a3, ...). See this answer.