is it possible to do something like this in Java? I’m just wondering.
First, I just create a new thread that has one argument.
Thread thread = new Thread(new Person());
Then, in the constructor of Person() I would like to start that thread.
So is something like this possible?
public Person() {
// Here belongs some code for the constructor and then
// I would like to start the thread
}
No, you can’t. Before Java can call
Thread()constructor it first has to eagerly evaluate all arguments, including call toPerson()constructor. This means that by the timePersonconstructor is called, the outerThreadobject doesn’t even exist or was not yet initialized so you cannot really use it.