You should look up some tutorials or books on "anonymous inner classes". They are classes that are created on the fly (often in cases of listener events). It has its own special rules and abilities beyond that of normal classes.
If I remember correctly, you're not really instantiating an interface so much as you are instantiating an anonymous inner class that implements the interface, although its been awhile since I've reviewed the material.
I think the code you are refering to in your second question is a little hazy in your memory. I suggest you find it again and post the exact code.
As for the first question, I think you are referring to a instance initializer. See the JLS, section 8.6. Think of it as a common block of code executed by constructors -- it's like a generalized instance variable initializer. There are rarely used and usually to "be the constructor" in an anonymous class. Demo: [ December 15, 2005: Message edited by: Jeff Albrechtsen ]
Originally posted by Scott Selikoff: ... If I remember correctly, you're not really instantiating an interface so much as you are instantiating an anonymous inner class that implements the interface...
Correct. And the reference is upcast to the interface type.
Basically, an anonymous class looks like a constructor call, but before the closing semicolon, a class definition is slipped in with braces. The result is a class that implicitly extends the class (or implements the interface) identified after the keyword "new." The reference to this anonymous class instance is automatically upcast to the type it extends (or implements).
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Mhh, I use anonymous inner classes all the time...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Sep 16, 2005
Originally posted by Ilja Preuss: Mhh, I use anonymous inner classes all the time...
Me too. I think it was the instance initializer that was being singled out, eh? [ December 16, 2005: Message edited by: Jeff Albrechtsen ]
Joined: Jul 11, 2001
Originally posted by Jeff Albrechtsen:
Me too. I think it was the instance initializer that was being singled out, eh?
I see. I didn't use them either, until someone showed me that they can neatly be used to initialize a collection (for example in constant declarations):
Joined: Dec 15, 2005
Yes, the examples showing an anonymous subclass passed to the superclass/interface constructor match the code example I saw.
Now I understand what's going on there. Many thanks to those who replied.