This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Reg. runnable - interface??!" Watch "Reg. runnable - interface??!" New topic

Reg. runnable - interface??!

Ravi Chilakapati

Joined: Apr 20, 2010
Posts: 10
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?

Matthew Brown

Joined: Apr 06, 2010
Posts: 4338

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

Joined: Apr 20, 2010
Posts: 10
Hmm.. interesting.. it makes a lot more sense now. Thank you!
I agree. Here's the link:
subject: Reg. runnable - interface??!
Similar Threads
Thread ...........Runnable
K & B: Inner Classes Self-Test
object of an interface? Hows possible
Annonymous Inner class problem