Re: Explain this Technique -- new() followed by an anonymous class declaration
On 2005-09-16, SMAccount <SMAccount@xxxxxxxxx> wrote:
> I took the following example from the Sun Java Tutorial on writing
> Swing Programs (see link below). The code invokes "new Runnable()"
> followed by an anonymous class declaration. I am new to Java, and I
> have never seen anything like this in any other language. Anyone know
> where there is any documentation on this type of technique? When you
> can use it? What are its' limits?
[snip]
See the "Class Instance Creation Expressions" section of The Java
Language Specification, say at
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#41147
for the documentation, which includes the information that when doing
a "new" on "the name of an interface then an anonymous direct subclass
of Object that implements the interface" is declared.
You can use it where every you can use a ClassInstanceCreationExpression.
Its limits are those of an anonymous class, such the class not being able
to have an explicitly declared constructor.
.
Relevant Pages
- Re: Interesting Java interview question
... active news group. ... the innnermost }-- inside of an anonymous class body. ... new Object{// Anonymous class declaration ... You've declared a constructor as well. ... (comp.lang.java) - Re: static hashtable with conent?
... Having no money invested in the reading of a post leaves the reader free to quit it at a moment's notice for any reason at all with no sense of having lost any investment. ... and certainly to avoid unreasonable demands), that Patricia, whose general good sense is, I should think, well established in these parts, at least to the extent that a history of posts can be said to establish a picture of their author, has the right of it here; that while the grammars that underlie both natural languages like English and artificial languages like Java are capable of forming expression and statements of arbitrarily high complexity, simply by applying the generation rules repeatedly, that in both cases the idioms which are understood easily and naturally come from a constrained application of those rules, and that using examples lying outside the standard idioms places upon our readers, whose ready understanding is in fact in our own interests as well as in theirs, an unnecessary burden, and thus should be avoided or at least severely minimized, absent any significant advantage to be found in their use. ... In some rare cases, although it has yet to be necessary for any program I've worked on, I can imagine needing to define an anonymous class constructor. ... It's just that in Java the anonymous class idiom is the heart of certain closure-like expressions. ... (comp.lang.java.programmer) - Re: Passing a method(reference) to an other method and calling the method.
... I do like the anonymous class idiom, and various other places where Java uses subtypes to do what closures or delegates would. ... I agree that it's nice to have the code that's being "passed" (whether as a class implementing an interface or a delegate) present where it's used. ... (comp.lang.java.programmer) - Re: Explain this Technique -- new() followed by an anonymous class declaration
... > public void run{ ... certain interface, but the actual code that implements the methods declared ... create an anonymous class and put the appropriate code right there. ... you can simulate passing functions around as ... (comp.lang.java.help) - Java "interface" vs. OO interface
... often wondered if java made a mistake here (let's not let this descend into ... Java has a facility called literally the "interface", ... /kids/ declaration I'm only able to access the members of the interface: ... (comp.object) |
|