1

Java allows using a local variable in the lambda expression, which is an implementation of a functional interface's method. On the other hand, it doesn't allow the same if the same interface's method is implemented in a class. Why?

shobhit
  • 57
  • 6
  • It's called a Closure. You can read about it [here](https://www.bruceeckel.com/2015/10/17/are-java-8-lambdas-closures/). – Robert Harvey Jan 05 '23 at 18:55
  • 1
    "*On the other hand, it doesn't allow the same if the same interface's method is implemented in a class."* --- [The java compiler disagrees with this statement (`Ideone.com`)](https://ideone.com/wYfN0l). – Turing85 Jan 05 '23 at 18:57
  • @Turing85: That is an anonymous function; it's essentially doing the same thing as the lambda expression (namely, closing over the local environment). – Robert Harvey Jan 05 '23 at 18:59
  • @RobertHarvey It is an anonymous class, yes. And of course it does. That's why the statement in the post is not correct; on a semantical level, they are interchangable. – Turing85 Jan 05 '23 at 19:00
  • Very likely the OP is trying to articulate something else. – Robert Harvey Jan 05 '23 at 19:01
  • 1
    @RobertHarvey it doesn’t matter whether the class is anonymous, [here’s a derived example with a named class](https://ideone.com/Y6QoQl). Of course, the variable still must be within the lexical scope, but how would you interpret this question when there isn’t a variable? – Holger Jan 06 '23 at 17:58
  • @Holger: I never said it did. As to your question, you'd have to ask the OP that. – Robert Harvey Jan 06 '23 at 20:58
  • 1
    @RobertHarvey the point is, Turing85’s statement is correct no matter how we interpret the question. All that matters, are the source code location and whether the variable is (effectively) final. There is no difference between lambda expressions and classes, anonymous or named, in this regard. – Holger Jan 07 '23 at 13:45

1 Answers1

0

when you use a lambda expression to create an instance of a functional interface, the Java compiler effectively creates an anonymous inner class that implements the functional interface. As part of this process, the Java compiler captures the values of any variables that are used in the lambda expression. That's why you can use local variables within a lambda expression as long as they are effectively final.

On the other hand, when you implement a functional interface in a class, the class is not anonymous and is not created at the point where the interface is implemented. Therefore, the Java compiler does not have the opportunity to capture the values of any variables that are used in the implementation of the functional interface's method.

Roniel López
  • 403
  • 2
  • 10
  • "*when you use a lambda expression to create an instance of a functional interface, the Java compiler effectively creates an anonymous inner class that implements the functional interface.*" - It is true on a semantical level. [It is not true on a technical level, lambda are implemented separtely](https://stackoverflow.com/questions/57029375/is-lambda-in-fact-an-anonymous-class). – Turing85 Jan 05 '23 at 19:05