Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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.
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!