• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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.
 
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!