Page 1 of 1

About Question enthuware.ocajp.i.v7.2.921 :

Posted: Fri Jul 18, 2014 9:16 am
by kashyapa
I think the reference variable 'mc' in Myclass's getMyClassObject() still hanging that object.

Because the return statement returns the reference to that object and not the object. So both variables (mc,x) pointing to the same object. After line 6 the variable 'x' turn his point to another object, but variable 'mc' still hanging previous object.

couldn't it happen?

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Fri Jul 18, 2014 9:21 am
by admin
Once a method returns all the variables that it created are gone. The stack space (where all the automatic variables are kept) is cleared up and reclaimed. So there is no question of a variable hanging on to an object after the method returns.

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Sun Aug 10, 2014 10:34 am
by hamada.yamasaki
Isn't it that the object will become eligible after line 6 ,because at line 6 the assignment is taking place?

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Sun Aug 10, 2014 7:50 pm
by admin
Yes, and that is what the question says, "After what line...".

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Tue May 30, 2017 6:50 am
by Javier
Hi Paul!
I would like to know why is not collected the object after the line 2.
Perhaps it is because at that moment the program is not run, only declared, so there is not object created...is it right Paul?
Thank you very much!!

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Tue May 30, 2017 11:47 am
by admin
No, that is not correct. The reference to the object created at //1 is being returned back to the caller of getMyClassObject method at //2. Thus, the object will still be referenced by an active variable even after getMyClassObject method returns. That is why it cannot be garbage collected at //2.

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Sat Jun 03, 2017 11:02 am
by Javier
Hi admin!
and without main method, just with the TestClass{...}
it is the object created? it is necessary to have main method to create objects?
Thank you very much!

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Sat Jun 03, 2017 11:09 am
by Javier
And my second question is:(sorry that I didn´t put it on the last one)

so if the getMyClassObject() does not return the mc variable, the created object in this method would be eligible for GC?

Thanks a lot!

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Sat Jun 03, 2017 10:32 pm
by admin
1. No, main method (or any method) is not required to create an object. An object is created when you use the new keyword. The main method is required only to execute a java class from the command line. But objects are created at run time. So you need some mechanism to run the program. In this case, the main method is executed by the jvm and the object will be created upon execution of the class.
2. Yes, it will be eligible for gc if the reference to that object is not returned.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Mon Jun 05, 2017 2:19 pm
by Javier
Thank you very much Paul!!

I have much more clear now :)

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Mon Jul 10, 2017 10:22 am
by kharesoft
I thought the unassigned string in SOP method of line 5 should be eligible for garbage collection.

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Mon Jul 10, 2017 9:47 pm
by admin
No, it is a String literal and it will be interned. It will not be GCed.
You may read more about it here: https://dzone.com/articles/string-inter ... at-why-and

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Wed Dec 05, 2018 7:02 pm
by OCAJO1
if the code in the main() were to change to the following,

Code: Select all

class MyClass { }
public class TestClass{
                MyClass getMyClassObject(){
                            MyClass mc = new MyClass(); //1
                            return mc; //2
                }

                public static void main(String[] args){
                          TestClass tc = new TestClass();
                          MyClass x;
                          tc.getMyClassObject();
                          System.out.println("got myclass object");
                          x = new MyClass();
                          System.out.println("done");
               }
}
then the object created in line //1 would be eligible for gc in line //2.

Is that correct?

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Wed Dec 05, 2018 10:34 pm
by admin
No, if a method is returning a reference of an object, then that object cannot be GCed before the method ends.

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Thu Dec 06, 2018 2:18 pm
by OCAJO1
So we're talking about scope of the mc reference. Are we not?

Ignoring that the method was called from main(), Am I correct in saying that in both original version and this version, the object in line //1 becomes gc eligible outside getMyClassObject(), because scope of the mc ends?

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Thu Dec 06, 2018 4:30 pm
by admin
Not sure what is your point. scope of mc is only within the method.

>the object in line //1 becomes gc eligible outside getMyClassObject(), because scope of the mc ends?

no, what has scope of mc got to do with this??

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Thu Dec 06, 2018 6:11 pm
by OCAJO1
admin wrote:
Wed Dec 05, 2018 10:34 pm
No, if a method is returning a reference of an object, then that object cannot be GCed before the method ends.
Ok let me ask this way - in your response above, by method, aren't you referring to getMyClassObject()?

If that is so, then when it comes to gc, does a method end when the program ends, or when the last call is made to it?

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Thu Dec 06, 2018 10:45 pm
by admin
OCAJO1 wrote:
Thu Dec 06, 2018 6:11 pm
admin wrote:
Wed Dec 05, 2018 10:34 pm
No, if a method is returning a reference of an object, then that object cannot be GCed before the method ends.
Ok let me ask this way - in your response above, by method, aren't you referring to getMyClassObject()?

If that is so, then when it comes to gc, does a method end when the program ends, or when the last call is made to it?
1. It is a general statement. It is applicable to all methods and not just this one.

2. Neither of your options is correct. Method ends every time it is invoked and it executes a return statement. If there is no return statement, it ends after the list line of the code in the method.

I don't know what you are thinking when you say, "or when the last call is made to it?".

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Tue Jan 08, 2019 4:37 pm
by crazymind
The reference is no more exist after method ends. So returned object will be GCed if we don't assign the return object back a new reference.

Is this logic correct?

Re: About Question enthuware.ocajp.i.v7.2.921 :

Posted: Tue Jan 08, 2019 9:41 pm
by admin
crazymind wrote:
Tue Jan 08, 2019 4:37 pm
The reference is no more exist after method ends. So returned object will be GCed if we don't assign the return object back a new reference.

Is this logic correct?
Yes, that is correct.