Originally posted by Rajesh Chandra:
Thanks for the reply.I know its anonymous class but the new keyword along with interface name confuses me.
Any way is this legal??
Runnable r = new Runnable();
r.run();
I read its legal. if so why
cheers
Rajesh
This is not legal. Did you try to compile it? Since Runnable is an interface, you cannot instantiate it. However, you CAN create instantiate an anonymous inner class that
implements Runnable like this:
You should note that this code will compile and run. However, Runnable is designed to be used with the
Thread class for multi-threaded programs. The problem is that the above code does not use Thread, so the call to r.run() is still running on the main thread. This is not the main point of your question, though, so I'll just let you look into threads more on your own to understand why this is not a good idea.
Layne