Search code examples
javainstantiationjava-threads

Does the way of instantiating a new Thread make a difference?


What are the difference (if any) between instantiating a Thread like this

SomeThread t = new SomeThread();
t.start();

and like this:

new SomeThread().start();

Will the constructor in class SomeThread still be instantiated?

Or does it actually skip instantiating the constructor the second time and go straight for the run()- method?

Note: The class SomeThread extends Thread


Solution

  • The new keyword is used for object creation, and you are still calling the constructor, as you can see with the call to SomeThread(). The two code examples above are equivalent.