Anonymous inner classes

J

Jon Harrop

I've been hearing about Java's anonymous inner classes a lot recently.
What's the difference between these and closures?
 
T

Tom Hawtin

Jon said:
I've been hearing about Java's anonymous inner classes a lot recently.
What's the difference between these and closures?

Briefly, anonymous inner classes:

* have a hideously over-verbose syntax
* cannot assign local variables (use non-finals)
* cannot break/continue/return through to outer context
* 'this' references the wrong object
* may have multiple methods and instance fields
* can subtype interfaces and classes

In a perfect language you would be able to take a block of code and wrap
it as a closure without changing any of the text of the block itself.

For my money, it's worth fixing the first two points, and warning on an
unqualified this.

Tom Hawtin
 
L

Lew

Tom said:
Briefly, anonymous inner classes:

* have a hideously over-verbose syntax
* cannot assign local variables (use non-finals)
* cannot break/continue/return through to outer context
* 'this' references the wrong object
* may have multiple methods and instance fields
* can subtype interfaces and classes

In a perfect language you would be able to take a block of code and wrap
it as a closure without changing any of the text of the block itself.

For my money, it's worth fixing the first two points, and warning on an
unqualified this.

This is the most cogent and compelling argument for support of closures in
Java that I have encountered. I am swinging over to the Darkseid on this -
yay, closures!

- Lew
 
J

Jon Harrop

Tom said:
Briefly, anonymous inner classes:

* have a hideously over-verbose syntax
* cannot assign local variables (use non-finals)
* cannot break/continue/return through to outer context
* 'this' references the wrong object
* may have multiple methods and instance fields
* can subtype interfaces and classes

Ok, so they really screwed this one up. :-(
In a perfect language you would be able to take a block of code and wrap
it as a closure without changing any of the text of the block itself.

Yes, that's what I'm used to.
 
B

Brian Palmer

Jon Harrop said:
Ok, so they really screwed this one up. :-(

Anonymous inner classes are not closures, correct; but they solve a
need that java has for simple objects that work within the type
system. They're very useful for what they do.
 
M

Mike Schilling

Anonymous inner classes are not closures, correct; but they solve a
need that java has for simple objects that work within the type
system. They're very useful for what they do.

What follows is my opinion only:

The Java 1.2 event system required a callback mechanism more flexible and
dynamic than "implement an interface for every callback you want to
receive". The simplest and most familiar to programmers would have been
method pointers of some sort. But Sun, then in the position of suing
Microsoft for daring to add method pointers to Java, could not afford to do
so itself. Thus, anonymous classes, which serve the same purpose, and the
famous (and unconvincing) White Paper explaining their superiority.

http://java.sun.com/docs/white/delegates.html#Why
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top