• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!