Closure is a Closure class instance, that implements Call logic.
It may be passed as argument or assigned to a variable. It also has some logic concerned with scope variable accessing and delegating calls.
Methods are normal Java methods. Nothing special.
And yes, anonymous inner classes have a lot of boilerplate code to perform simple actions.
Compare:
button.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e ) {
frame.dispose();
}
}
);
vs
button.addActionListener { frame.dispose() }
There is a related question on SO Groovy : Closures or Methods
and the following link(s) to the user guide containing a lot of useful information.
- http://groovy-lang.org/closures.html
A closure in Groovy is an open, anonymous, block of code that can take
arguments, return a value and be assigned to a variable. A closure may
reference variables declared in its surrounding scope. In opposition
to the formal definition of a closure, Closure in the Groovy language
can also contain free variables which are defined outside of its
surrounding scope. While breaking the formal concept of a closure, it
offers a variety of advantages which are described in this chapter.