Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Closures in Java 8  RSS feed

 
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!