The Buddy object created at line number 2 is set into instance variable upper at line number 3.
Now the object that will be returned from following constructor will remain till the program ends.
>number 2 is set into instance variable upper at line number 3.
No, at //2 the number variable refers to the method parameter and not the instance variable.
This has now been added to the explanation.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
No, it is correct. At //2, when you set upper = b, the reference to the Buddy object created at //1 is not stored in the instance variable upper. It is stored in the method parameter named upper, which is lost after the method returns (i.e. after //2 ).
If you like our products and services, please help us by posting your review here.
I'm still not convinced. upper = b; what it actually does is set upper reference at the same as b which is Buddy object. Now we have two references to Buddy: b and upper. No reference to Buddy is lost. Take a look at this code executed at https://www.jdoodle.com/online-java-compiler/
Right, b and upper will both reference to the same Buddy object that was created at //1. But after line //2, both of these references are not used and so this object will be eligible for gc after that line because the method ends there.
If you like our products and services, please help us by posting your review here.
Why Object created at line //3 will be eligible for garbage collection after line //5? Is this because the program is end?
In this case Object created at line //1 will be eligible for garbage collection after line //5. is also a correct case.
Yes, you could say that but option 1 is better. We will update the problem statement to say that what is the "earliest" line after which the object is eligible for gc.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.