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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
kashyapa
Posts: 23
Joined: Thu May 08, 2014 5:27 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

hamada.yamasaki
Posts: 17
Joined: Fri Oct 11, 2013 10:31 am
Contact:

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

Post by hamada.yamasaki »

Isn't it that the object will become eligible after line 6 ,because at line 6 the assignment is taking place?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, and that is what the question says, "After what line...".
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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!!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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!

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post 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!

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Thank you very much Paul!!

I have much more clear now :)

kharesoft
Posts: 1
Joined: Tue Jun 27, 2017 8:15 am
Contact:

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

Post by kharesoft »

I thought the unassigned string in SOP method of line 5 should be eligible for garbage collection.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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?
Last edited by admin on Wed Dec 05, 2018 10:33 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code] tags

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

No, if a method is returning a reference of an object, then that object cannot be GCed before the method ends.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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??
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?".
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests