A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Spring in Action
this week in the
Programmer Certification (SCJP/OCPJP)
Difference between passing object to constructor?
Joined: Dec 19, 2008
Dec 28, 2009 10:51:42
What is technical difference if I pass Runnable in two different way. Please explain..
Thread thread = new Thread (new RunnableClass());
RunnableClass runnable = new RunnableClass();
Thread thread = new Thread(runnable);
Thanks in Advance...
SCJP 6.0 Certified 96%
Joined: Feb 23, 2009
Dec 28, 2009 10:52:49
The difference is that you'll have another reference variable poiinting to the RunnableClass instance. Nothing else.
An easy to use, light-weight JDBC framework
Joined: Sep 06, 2009
Dec 28, 2009 22:23:09
As mentioned by Sebastian, the difference is that you would have reference of runnable object in the second case. The second syntax is preferred when we want to create multiple threads on the same runnable instance.
It is sorta covered in the
JavaRanch Style Guide
subject: Difference between passing object to constructor?
how to use join method so that thread1 should wait for thread2?
why run() method in Runnable interface is not called ?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014