Page 1 of 1
About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Jan 16, 2021 9:30 am
by ravi2519
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.
Code: Select all
public Buddy(String name, Buddy upper)
So i am confused as to why and how this option is correct:
Code: Select all
Object created at line //1 will be eligible for garbage collection after line //2
And this option incorrect:
Code: Select all
Object created at line //1 will be eligible for garbage collection after line //5
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Jan 16, 2021 11:38 pm
by admin
>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!
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Fri Sep 03, 2021 5:42 am
by pblanchardie
There is a typo, the variable name is upper (instead of number)
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Fri Sep 03, 2021 7:30 am
by admin
You are right. Fixed.
thank you for your feedback!
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Fri Nov 04, 2022 1:54 pm
by lucash9
I think that should be
instead of
to to satisfy the correctness of first answer.
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Nov 05, 2022 1:42 am
by admin
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 ).
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Nov 05, 2022 3:56 am
by lucash9
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/
Code: Select all
public class Buddy {
public static void main(String args[]) {
Buddy b1 = new Buddy("A"); // 3
Buddy b2 = new Buddy("B", b1); // 4
//System.out.println(b1); // 5
}
Buddy upper;
String name;
public Buddy() {}
public Buddy(String name) {
this.name = name;
}
public Buddy(String name, Buddy upper){
this.name = name;
Buddy b = new Buddy(upper.name);//1
System.out.println("b reference before line 2: "+b);
upper = b; //2
System.out.println("b reference after line 2: "+b);
System.out.println("upper reference: " +upper);
}
}
As a result we will see something like this
b reference before line 2: Buddy@3b192d32
b reference after line 2: Buddy@3b192d32
upper reference: Buddy@3b192d32
which indicates that b still referenced to Buddy.
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Nov 05, 2022 4:10 am
by lucash9
Maybe you mean a situation after method is called so that local Buddy pointed by b and upper is not stored anywhere so it will be eligible for GC?
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Nov 05, 2022 4:16 am
by lucash9
It certainly is. Silly me.
Re: About Question enthuware.ocpjp.v11.2.3616 :
Posted: Sat Nov 05, 2022 7:10 am
by admin
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.