Java Reference
In-Depth Information
As the comments indicate, num is effectively final and can, therefore, be used inside
myLambda . This is why the println( ) statement outputs the number 18. When func( ) is
called with the argument 8, the value of v inside the lambda is set by adding num (which is
10) to the value passed to n (which is 8). Thus, func( ) returns 18. This works because num
is not modified after it is initialized. However, if num were to be modified, either inside
the lambda or outside of it, num would lose its effectively final status. This would cause
an error, and the program would not compile.
It is important to emphasize that a lambda expression can use and modify an instance
variable from its invoking class. It just can't use a local variable of its enclosing scope un-
less that variable is effectively final.
Search WWH ::




Custom Search