File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between passing object to constructor?" Watch "Difference between passing object to constructor?" New topic

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());


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: 253

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.

Salil Verma
I agree. Here's the link:
subject: Difference between passing object to constructor?
Similar Threads
Threads Queries
how to use join method so that thread1 should wait for thread2?
why run() method in Runnable interface is not called ?