In article <9f5avq$9ls$1@soapbox.cs.bham.ac.uk>,
Aaron.Sloman.XX@cs.bham.ac.uk (Aaron Sloman See text for reply address) writes:
> I don't know how anonymous classes work in Java,
So far as I can tell, to allow you to implement things with almost
all of the semantic effects of closures with almost none of the
syntactic convenience.
> but I guess there are
> at least three reasons for using anonymous classes:
>
> (a) privacy: the class and its methods can be accessed only in the
> scope of the text where the class is introduced.
Well, nested classes do that; what anonymous classes do is allow you
to write a class "in-line" at the point where you `new` it, rather
as a procedure ... endprocedure expression allows you to write a
procedure expression where you need it rather than as a definition.
> (b) efficiency because global identifiers don't need to be created
Certainly not true for Java classes!
> (c) syntactic convenience
If only.
Well, anonymous classes are more convenient than named classes when
they're appropriate. For small values of "more".
(Actually there are three interacting aspects to this in Java: nested
classes (if not static) have some closure-like properties, as their
instances can "see" the instance of the surrounding class that "owns"
them; nesting classes does scope control; and anonymous classes allow
you to write a class body after "new" so you don't need to park a
class definition a "long way off" in the text.)
I'm not sure the existing language design and implementation constraints
(eg it must compile to code that works on the older JVMs) left them
much room for manoeuver).
--
Chris "but I strongly dislike the result" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
|