This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Interfaces and instantiation  RSS feed

 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I am studying for Java certification. I ran Mughal Khalid's exam applet, where I learned the following:
------
Given that a static method doIt() in a class called Work represents work to be done, the following will successfully start a thread that will do the work:
Runnable r = new Runnable() {
public void run() {
Work.doIt();
}
};
Thread t = new Thread( r );
t.start();
------
It looks to me like the interface Runnable is being instantiated in the first line, yet I have learned that instantiating interfaces in Java is verboten.
Any clearing of this issue that confuses me would be greatly appreciated.
Thank you,
Art
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Art,
The code isn't instantiating Runnable, it's creating an instance of an anonymous inner class that implements Runnable. There's a lot to be said about nested classes, so I'm not going to give a tutorial here - it'd run too many pages. Check out Just Java or Core Java vol. 1 for a good intro.
jply
 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Jerry is saying that the object r is actually an object of a class that implements Runnable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!