Explicit call of Runnable.run
- by klaudio
Hi, I have a question.
Somebody, who was working on my code before me, created some method and passed Runnable as parameter, more likely:
void myMethod(Runnable runnable){ runnable.run(); }
Then calling myMethod out of main looks like:
public static void main(String args[])
{ try
{ myMethod(new Runnable(){ public void run() { //do something...; }}); }
catch (Throwable t) { } }
So, to supply parameter to myMethod I need to instantiate object of (in this case anonymous) class implementing Runnable.
My question is: is it necessary to use Runnable in this example? Can I use any different interface? I mean I can create new interface with single method i.e.
interface MyInterface{ void doThis(); }
then change look of myMethod: void myMethod(MyInterface myObject){ myObject.doThis(); }
And of course client too:
public static void main(String args[]) {
try { myMethod(new MyInterface (){ public void doThis()
{ //do something...; }}); }
catch (Throwable t) { } }
Or maybe something is about Runnable?!