File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Groovy and the fly likes Closures and Groovy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Closures and Groovy" Watch "Closures and Groovy" New topic

Closures and Groovy

Trevor Butler

Joined: Sep 29, 2006
Posts: 2

Hello again - it's Trevor Butler from SAS. I enjoyed your last class here on Java 5, and at the end, you brought up Groovy and gave us an introduction.

I recently had the need to perform a search and replace on a text file to automate some testing I am doing, so I loaded up the Groovy jar and got to work. In combing through the codehaus site and a Groovy book a colleague had, I was fascinated by closures. Coming from Java, most of the Groovy language made sense either as a simpler syntax or shorthand. However, closures were different.

I have never used any other language that had closures, and it took me a while to get the hang of them. I still am not entirely comfortable with them beyond basic for-each type looping scenarios.

Since I found closures to require a paradigm shift in thinking, I am guessing other folks coming from Java and other static OO languages might feel the same.

Question: How would you describe closures to a person such as myself who comes from a Java / static OO / procedural language world in such a way that they can grasp the concept?

S Davis
Ranch Hand

Joined: Feb 07, 2006
Posts: 40
Hi Trevor -- good hearing from you again.

Closures are a bit of a conceptual leap for Java developers. (Bear in mind that closures are due to be added to Java 1.7, so thanks to Groovy you're getting a bit of a sneak preview now... grin)

The easiest way to think of a closure is a freestanding block of code that isn't attached to a class. Things that you might consider making a static method on a utility class can be declared as a closure instead. So rather than the anonymous blocks of code we're used to seeing surrounded in curly braces after if statements, while loops, try/catch blocks, etc, can now be stored in a named variable and called at will.

All metaprogramming that you do involves closures. If you want to add a new method to a class, you create a closure and add it to the classes ExpandoMetaClass.

All of the "methods" in a Grails controller are, in fact, closures. Simply named blocks of code.

Here are a few links that might help:
Informal Guide to Closures
Formal Guide to Closures
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
[Scott]: Bear in mind that closures are due to be added to Java 1.7

Hm, have you heard indications that that's definite? My impression is it's tentative - a lot of people are interested in getting some closure-like feature(s) into JDK 1.7, but there's still disagreement about how to do it, and also a fair amount of resistance to the idea. Seems to me like there's a fair chance this might get pushed to later, or possibly dropped entirely. I hope not, but I don't know.

"I'm not back." - Bill Harding, Twister
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1850

Closures in Groovy

Krishna Srinivasan
OCAJP 7/8 Exam, 400+ OCPJP Mock Questions
I agree. Here's the link:
subject: Closures and Groovy
jQuery in Action, 3rd edition