In what situation would anyone ever use the no-argument constructor of the Java Thread class? The API says:
This constructor has the same effect as Thread(null, null, gname), where gname is a newly generated name.
Correct me if I am wrong, but I think the target of the thread can not be modified after the new Thread object is instantiated. If the target equals null then the start method will do nothing right?
Why would you use this constructor?
In Java, a no-argument constructor is the default constructor and if you don't define explicitly in your program. Then Java Compiler will create a default constructor with no arguments. The purpose is to call the superclass constructor.
The arguments of a constructor can only be found by type, not by name, so there is no way for the framework to reliably match properties to constructor args. Therefore, they require a no-arg constructor to create the object, then can use the setter methods to initialise the data.
Though, In the case of Hibernate Persistent classes or Entities, it's must to provide a no argument constructor, so that Hibernate can create instance of Persistence classes, when you hibernate load them from database. It also uses newInstance() method to create instance of persistent classes.
No-argument constructor: A constructor that has no parameter is known as the default constructor. If we don't define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class.
For one thing, it allows you to create subclasses without the PITA of explicitly calling the superclass constructor, e.g.
new Thread(){
public void run() { ... }
}.start();
If you make a (perhaps anonymou) class that inherits Thread
and overrides run
, your class needs to call this base constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With