• Post Reply Bookmark Topic Watch Topic
  • New Topic

anonymous blocks  RSS feed

 
Barry Gold
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was looking at a chunk of code in a test, and it had unnamed blocks of code apparently being passed as parameters to a function (or something of that sort).

I don't have the exact code in front of me, but it looked something like


I looked through all the Sun docs trying to find what this is about, but couldn't hit on the right keyword for searching, because I have no idea what it's called or how it's supposed to work.

The same question also had something like:


I thought you weren't allowed to instantiate an interface.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah the things I remember from studying for the SCJP that I *never* use...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the darker corners of the language. Or would that be making a static method *final*?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mhh, I use anonymous inner classes all the time...
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):

 
Barry Gold
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!