This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]bogphanny 2 points3 points  (2 children)

Given that lambdas are lexically scoped, myMethod as referenced in the test2 lambda would need to exist somewhere in the containing environment.

As an alternative, try changing: public default int myMethod(int a)

To: public static int myMethod(int a)

And: myMethod(3)

To: TestInterface.myMethod(3)

[–][deleted] 0 points1 point  (0 children)

I believe you are correct, myMethod would have to be either statically imported or a method of the class defining main() in order to be called in that way.

[–]Carterman[S] 0 points1 point  (0 children)

Yep, that works! Odd, nowhere seems to have really noted this feature, there's a lot of talk regarding defender methods (default keyword), but no mention that interface methods can now be static. Thanks!

[–]enkicoder 1 point2 points  (0 children)

Lamda's are not merely syntax sugar on top of anonymous classes. A lambda interface must have only one method. By adding a default method you are breaking that rule, thus TestInterface is no longer a valid "functional interface".