Why can we not use default methods in lambda expressions?

It’s more or less a question of scope. From the JLS

Unlike code appearing in anonymous class declarations, the meaning of
names and the this and super keywords appearing in a lambda body,
along with the accessibility of referenced declarations, are the same
as in the surrounding context (except that lambda parameters introduce
new names).

In your attempted example

Formula formula = (a) -> sqrt( a * 100);

the scope does not contain a declaration for the name sqrt.

This is also hinted at in the JLS

Practically speaking, it is unusual for a lambda expression to need to
talk about itself
(either to call itself recursively or to invoke its
other methods), while it is more common to want to use names to refer
to things in the enclosing class that would otherwise be shadowed
(this, toString()). If it is necessary for a lambda expression to
refer to itself (as if via this), a method reference or an anonymous
inner class should be used instead.

I think it could have been implemented. They chose not to allow it.

Leave a Comment