You use closures when you need to 'pass' a method to another object.
Threads are a good example. When you start a new thread, that thread will want to know what to execute. In other languages, like C, we can simply pass our function directly to the thread. We say, "here is the address of the function you should execute". Sadly, you can't do this in Java. In Java, you can give a thread something to execute by making a closure and passing it to the thread. In Java, we call these closures "Anonymous classes". Here is an example:
Just stick with normal methods. You'll see for yourself when closures become useful.
I find clojure methods useful when I don't have to retain a named object. In otherwords I'm passing on a behaviour, but don't need to retain a copy of the object that has the behaviour.
For example, with a button action:
The advantage in this case is that I don't have to write something else that implements ActionListener, all of the code is very visible, so for maintenance you're not traversing compilation modules to determine behavior, and I don't have a reference to something that I really don't need. If the code is complex and/or large it may be beneficial to break it out into a seperate class for readability.
Hope this make sense.
Stephan van Hulst
Joined: Sep 20, 2010
Sorry for being a nitpicker, but I want to remind everyone that the mechanism is known as a closure, and that Clojure is the name of a functional language.