Re: Callbacks, and using interfaces as arguments



The second one is implementing an anonymous inner class in these lines:

addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

The outermost { and } above are the boundaries of the class, and the
class extends WindowAdapter. I personally don't like this syntax, and
always make it a separate top-level class, but many like it, because
it's brief. However, it isn't simple. An anonymous inner class always
maintains a reference to the enclosing instance, which can affect
garbage collection. It can access private members of its enclosing
class (which is implemented in terms of generated accessor methods),
which you might see as a break of encapsulation.

It can access local variables from the enclosing
method/constructor/initialiser, which have to be final (don't worry,
you'll get a compiler error telling you about this).

Because the class is anonymous, it does not promote reuse, i.e., you
can't easily refer to it again later to use the same thing somewhere
else.

There is more on this subject here:

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

.