• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runnable confusion

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following points are from K&B book..
---------------------------------------------
Be sure you remember the following: You start a Thread, not Runnable. You call start() on Thread instance, not on a Runnable instance.
If you see code that calls run() method on Runnable instance --its perfectly Legal.
----------------------------------------------
My question is Runnable is an Interface so how you create an instance of an Interface ??
Runnable r = new Runnable();
r.run(); //Legal ???..PLease explain !!
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

you can create a class that implements that interface : class MyClass implements Runnable

You cannot do "new Runnable()" though.
so you will rather declare your instance like this : Runnable r = new MyClass();
and do r.run();

This will actually call the run method from MyClass that you had to implement because MyClass implements Runnable.

Please anyone correct me if I'm wrong, I don't pretend to be an expert.
Alex
[ April 26, 2005: Message edited by: Alex Turcot ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Alex mentioned, it is possible to reference an instance via its interface.



If the instance is declared anonymously, it may only be reference that way. (Okay, so it can still be referenced as an Object instance)



Henry
[ April 26, 2005: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you could declare an anonymous that implements Runnable on the fly.

 
reply
    Bookmark Topic Watch Topic
  • New Topic