The term "closure" is defined as follows:
"In computer science, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. The explicit use of closures is associated with functional programming and with languages such as ML and Lisp. Constructs such as objects in other languages can also be modeled with closures"
The first paragraph is a little bit too abstract, but it is getting better:
"A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. However, the variable is of indefinite extent, so a value established in one invocation remains available in the next. As a consequence, closures can be used to hide state, and thus to implement object-oriented programming.
The concept of closures was developed in the 1960s and was first fully implemented as a language feature in the programming language Scheme. Since then, many languages have been designed to support closures."
Now it is getting clear - closure is something like a "crippled" method, which you can pass around. However this method is special - and has the access to the invocation scope - so you could access the state of the object, from which it is invoked. A closure is somehow similar to a inner class - with hopefully more straight forward syntax. Some observations:
- Closures are used mostly in functional languages
- The concepts of closures is about 50 years old
- Closures are especially useful for API design
Java isn't a functional language and the closure debate arised just in the recent two years. I don't think, that it would be possible to refactor big portions of e.g. the Java-libraries before Java 7. So we would have to live with the mix - legacy java.util and java.lang libraries with probably new, more concise designed Java 7 libraries. Some closure examples from Neal Gafter's blog.
I'm not sure, whether it is a really good idea to push Java in the functional direction - ono the other hand - with closures you can reduce a significant amount of code - which is always a good thing. What I observer too: many developers are getting really excited about closures - it is a really nice topic for getting warm in a meeting as well:-).