• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is this a closure?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to find out more about what is going on in the below code. I know that the new ExpectedCondition bit is some kind of callback but what exactly is this language feature called? Is it a closure/lamda expression? Where can I learn more about this kind of thing?



Cheers,
Jack
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Jack, and welcome to the Ranch!

No, that's not a closure. There was talk of adding closures to Java 7, but I don't think they made it in. Don't quote me on that last bit though.

What you're seeing is Java's closest approximation to a closure. It's an anonymous inner class.(⇐That's a google link, but skip anything that takes you to roseindia. That site is known for being rife with errors.)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 7 indeed does not have closures / lambda expressions. That's a new feature that will most likely be included in Java 8.
 
Saloon Keeper
Posts: 13891
314
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You *could* call it a kind of closure. Most people who are familiar with the term will know what part of the code you're referring to. You could also call this new ExpectedCondition a callback.

However, they are not. The new ExpectedCondition is called an Anonymous Class. The operation on xpathQuery is also not a closure or a lambda expression, even if does the same thing.

You get closures when you bind a function object to a variable mentioned in the lexical context outside the function. You get lambda expressions when you create an anonymous function type. You get a callback when you pass a function to another function.

Java doesn't have function objects so you can't have closures, lambda expressions, or callbacks. Anonymous classes provide a way of doing things that look a lot like these concepts though.
 
I found a beautiful pie. And a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic