Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Closures in Java 8

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are closures different in Java 8 compared to older versions of Java ?
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only difference is that you can now capture a local or parameter variable that is "effectively final"--that is, it is not modified after it has been initialized. You no longer need to declare it final. This works both in lambda expressions and in inner classes.

For example,



Here, the lambda expression captures the text and count parameters of the enclosing method that are effectively final.

As before, you cannot capture a variable. For example,



does not work.

Cheers,

Cay
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!