Consider below code,
class DemoStatic {
public static Runnable testStatic() {
return () -> {
System.out.println("Run");
};
}
public void runTest () {
Runnable r = DemoStatic::testStatic;
r.run();
}
}
public class MethodReferenceDemo {
public static void main(String[] args) {
DemoStatic demo = new DemoStatic();
demo.runTest();
}
}
run()
method of Runnable
instance that is being return by testStatic
method was supposed to be invoked.
And output on console should be "Run".
But this code is not invoking run()
method of instance r
and nothing is getting printed in console.
Can some one please explain the reason.
And comment if I am not using Method reference "::" properly.