my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Reg. runnable - interface??! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Reg. runnable - interface??!" Watch "Reg. runnable - interface??!" New topic
Author

Reg. runnable - interface??!

Ravi Chilakapati
Greenhorn

Joined: Apr 20, 2010
Posts: 10
Hi.
The code below is getting compiled.



I thought that Runnable was an interface? I read about Runnable at several places on the net, and yet, I fail to understand why this statement is valid.



I have observed that other interfaces can also be created like this.
Can someone please elaborate on this?

Regards,
Ravi.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

That's what's called an "anonymous inner class" (search for that and you'll find lots of references). You aren't creating an instance of Runnable (which is what you'd be doing without the {...} part). Instead you're creating an anonymous inner class that implements Runnable and creating an instance of that.

Look in the folder where the .class files go once you've compiled it, and you'll see there's an extra class in there.
Ravi Chilakapati
Greenhorn

Joined: Apr 20, 2010
Posts: 10
@Brown
Hmm.. interesting.. it makes a lot more sense now. Thank you!
Regards,
Ravi.
 
Don't get me started about those stupid light bulbs.
 
subject: Reg. runnable - interface??!
 
Similar Threads
Annonymous Inner class problem
Threads
Thread ...........Runnable
K & B: Inner Classes Self-Test
object of an interface? Hows possible