aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between passing object to constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between passing object to constructor?" Watch "Difference between passing object to constructor?" New topic
Author

Difference between passing object to constructor?

Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
What is technical difference if I pass Runnable in two different way. Please explain..

Thread thread = new Thread (new RunnableClass());

or

RunnableClass runnable = new RunnableClass();
Thread thread = new Thread(runnable);

Thanks in Advance...


Rajeev Kumar
SCJP 6.0 Certified 96%
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The difference is that you'll have another reference variable poiinting to the RunnableClass instance. Nothing else.


JDBCSupport - An easy to use, light-weight JDBC framework -
Salil Vverma
Ranch Hand

Joined: Sep 06, 2009
Posts: 255

Hi Rajiv,

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.


Regards
Salil Verma
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Difference between passing object to constructor?