fluffyBall
fluffyBall

Reputation: 23

instantiating a second class, will java wait till the second class's constructor is done, then move to the next line of code?

okay, i hope i can explain my question so its not hard to understand, i want to know if i can create an instance of a class in my main class but im not sure if the constructor of the second class lets my main class continue while the second class is busy with its constructor.

the reason i want to know this is, i want to access the data of the second class on the second line of my main class (right after the second class has been initiated) sorry for this weird question, i hope i didn't too bad explaining my question (im fairly new to it) thanks for reading!

Upvotes: 2

Views: 119

Answers (2)

Kaan
Kaan

Reputation: 5794

will java wait till the second class's constructor is done, then move to the next line of code?

Yes. Your application will run in a single thread by default (though it is possible to change this behavior). This means anything that happens in your code - method calls, loops, any calculation - they will occur serially; one thing after the other.

Upvotes: 1

Andreas
Andreas

Reputation: 159165

Unless you explicitly create multi-threading (by using any of the various ways Java supports for that), all the code runs in a single thread, so calling a constructor will always wait until the constructor is done.

Upvotes: 1

Related Questions