Page 1 of 2

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

Posted: Fri Jun 22, 2012 8:54 am
by georgeferreira
The question shows the follow code:

Code: Select all

public class NewClass{
   private Object o;
   void doSomething(Object s){  o = s;   }

   public static void main(String args[]){
      Object obj = new Object(); // 1
      NewClass tc = new NewClass(); //2
      tc.doSomething(obj); //3
      obj = new Object();    //4
      obj = null;    //5
      tc.doSomething(obj); //6
   }
}
At line 5 the seconde Object object lost all references, since question says "Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected, assuming no compiler optimizations are done?" the correct options shoud be line 5 or is GC only start on line 6 on Object?

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

Posted: Fri Jun 22, 2012 10:43 am
by admin
At //5 only the local variable obj is set to null but the original object created at //1 is still being referred to by the instance variable o of tc (Notice the call tc.doSomething(obj); which sets tc.o to point to the same object). This instance variable is changed to point to another object at //6. So it is only after //6 that the original object created at //1 is not being pointed to by any reference.

HTH,
Paul.

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

Posted: Tue Aug 21, 2012 12:31 pm
by javabeats
I think Line 6 is correct, because only after obj (which is now 'null') is passed to the doSomething() method, the original Object created on Line 1 will have no reference at all.

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

Posted: Fri Dec 07, 2012 7:05 pm
by ksnortum
The explanation in the exam is unclear; the one given here by the admin is much better.

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

Posted: Mon Jul 29, 2013 7:06 pm
by zkalev

Code: Select all

public class NewClass{
   private Object o;
   void doSomething(Object s){  o = s;   }

   public static void main(String args[]){
      Object obj = new Object(); // 1
      NewClass tc = new NewClass(); //2
      tc.doSomething(obj); //3
      obj = new Object();    //4
      obj = null;    //5
      tc.doSomething(obj); //6
   } 
}
In line //4 new Object is created and only "obj" pointing to this new object.In line //5 obj starts to point to null.Then isn't the new object from line //4 ready to be garbage collected.It's should be the same like this:

Code: Select all

  obj = new Object();    //4                                        Object obj = new Object(); 
      obj = null;    //5                         =>                                    obj = null;   

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

Posted: Mon Jul 29, 2013 7:43 pm
by admin
zkalev wrote: In line //4 new Object is created and only "obj" pointing to this new object.In line //5 obj starts to point to null.Then isn't the new object from line //4 ready to be garbage collected.
Yes, that is correct.
zkalev wrote: It's should be the same like this:

Code: Select all

  obj = new Object();    //4                                        Object obj = new Object(); 
      obj = null;    //5                         =>                                    obj = null;   
I am not sure I understand what you mean by this.

-Paul.

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

Posted: Tue Jul 30, 2013 1:33 am
by zkalev
I mean that I think line //5 is correct answer for the question....


P.S
But now I realize that I missed up that part from the question: object created on line // 1

Which is the earliest line in the following code after which the object created on line // 1 can be garbage collected, assuming no compiler optimizations are done?

Sorry I should be more careful!

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

Posted: Wed Aug 21, 2013 12:46 am
by javanaut
Could the object reference at line 5 be set to a new Object and the object originally created at line 1 still be eligible for garbage collection? So does it make a difference that line 5 is set to null? Does this just make the code and the instructions a little bit more confusing for the test taker? :lol:

:?

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

Posted: Wed Aug 21, 2013 9:12 am
by admin
javanaut wrote:Could the object reference at line 5 be set to a new Object and the object originally created at line 1 still be eligible for garbage collection? So does it make a difference that line 5 is set to null? Does this just make the code and the instructions a little bit more confusing for the test taker?

:?
Line 5 makes no difference. obj starts pointing to something else at Line 4 itself. It is there just to cause confusion :)

HTH,
Paul.

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

Posted: Wed Aug 21, 2013 12:17 pm
by javanaut
:D

Got it. Thank-you for the reply admin Paul. I thought this was the case, that the object instance variable with the private access modifier in the other class could be set to point to some other object not necessarily set to null to make it eligible for garbage collection. :D

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

Posted: Wed Mar 05, 2014 1:29 am
by fasty23
Is it correct: As obj starts pointing to something else at Line 4 that reference of obj from line 1 is eligible for gb at line 4? or because of "assuming no compiler optimizations are done" it not happen?
If we don't assuming no compiler optimizations are done, which line was true?
tnx

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

Posted: Wed Mar 05, 2014 2:13 am
by admin
Compiler optimizations can cause the compiler to not generate any code if it can figure out that the code is redundant. So it is not possible to predict. For the purpose of the exam, always assume that there are no compiler optimizations being performed.

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

Posted: Sat Sep 27, 2014 7:14 pm
by jbilkes
hi all

i choose line 4. Why? because i was thinking that when passing the object as argument to the doSomething, it is copied. And since the question is whether the actual original object would be eligible i thought after line 4 since there is only 1 unique (!) instance (by the way per definition the case for an object as far as i know) and that object is referenced to by obj and not by o (this is the copied version as believed). Since obj gets referenced to a new instance at line 4 this line is far from arbitrary IMHO

would like to hear your informed thought on this admin Paul

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

Posted: Sat Sep 27, 2014 7:47 pm
by admin
jbilkes wrote:hi all

i choose line 4. Why? because i was thinking that when passing the object as argument to the doSomething, it is copied.
There is fundamental flaw in your understanding of objects and references. Object is never copied while passing. References are.
Further, references are never garbage collected. Objects are.

Please go through the following links to clear up your understand and then this question will look really easy to you.
http://www.javaranch.com/campfire/StoryPassBy.jsp
http://www.yoda.arachsys.com/java/passing.html



HTH,
Paul.

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

Posted: Wed Sep 02, 2015 1:15 pm
by anief786
At Line 1: obj -> new object() @1234
At Line 2: o -> null;
At Line 3: tc.doSomething(obj); i.e., o, obj -> new object @1234
At Line 4: obj -> new object() @5678 , o-> new object @1234
At Line 5: obj -> null, new object() @5678 is eligible for garbage collection.

Line 5 would be the right answer. Could anyone clarify if i am anything wrong here, Thanks in advance?

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

Posted: Wed Sep 02, 2015 7:00 pm
by admin
>At Line 5: obj -> null, new object() @5678 is eligible for garbage collection.

The question is asking about the object created at //1 i.e. new object() @1234.
At //5, o is still pointing to 1234.

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

Posted: Wed Sep 09, 2015 7:35 am
by heleneshaikh
I do not understand why the object created on line 1 is not eligible for GC immediately after line 4.
obj = new Object(); // obj now points to a new Object and no longer refers to the Object created on line 1. Hence, the object created on line 1 has no references pointing to it at this point and -in my understanding- should therefore be eligible for GC.

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

Posted: Wed Sep 09, 2015 8:21 am
by admin
Because reference o is still pointing to that object.

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

Posted: Wed Mar 08, 2017 3:30 am
by Mihai1977
Hi,


Line 1: object "no1" is created, "obj" points to object "no1";
Line 2: object "NewClass" is created, "tc" points to object "NewClass";
Line 3: reference "o" point also to Object "no1";
Line 4: object "no2" is created, "obj" now points to "no2", "s" still points to object "no1";
Line 5: "obj" points to null, so "no2" is ready for garbage collector, "s" still points to object "no1";
Line 6: a new reference "o" points again to object "no1"???

Please explain me if I understood correct, thx.

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

Posted: Wed Mar 08, 2017 10:22 am
by admin
Mihai1977 wrote:Hi,


Line 1: object "no1" is created, "obj" points to object "no1";
Line 2: object "NewClass" is created, "tc" points to object "NewClass";
Line 3: reference "o" point also to Object "no1";
Correct.
Line 4: object "no2" is created, "obj" now points to "no2", "s" still points to object "no1";
s exists only inside the method doSomething. Once the method returns there is no "s" anymore at line 4.
But o, which is a instance member still points to object no1.
Line 5: "obj" points to null, so "no2" is ready for garbage collector, "s" still points to object "no1";
Again, there is no s at this point. But o, which is a instance member still points to object no1.
Line 6: a new reference "o" points again to object "no1"???
o is an instance member. So there is only one o per NewClass object. Since doSomething is being called on the same NewClass object, the same o will now be changed to point to null (because null is being passed to doSomething).

HTH,
Paul.

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

Posted: Thu Mar 09, 2017 1:11 am
by Mihai1977
Thx Paul, now it's all clear!

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

Posted: Fri May 04, 2018 2:01 pm
by flex567
I don't understand,
In line 5 no reference variable is pointing to the object created in line 1??
Why is the answer line 6?
Is it because parameter reference variable "s" is still pointing to the object?

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

Posted: Fri May 04, 2018 8:35 pm
by admin
The instance variable o of NewClass object still has as reference to the object created at line 1.

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

Posted: Sat May 05, 2018 2:30 am
by flex567
Aha, this one is pretty simple, but I got confused while reading so many comments.
Maybe some of the comments can be deleted.

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

Posted: Tue Dec 24, 2019 10:17 am
by morarumaria1988
admin wrote:
Fri Jun 22, 2012 10:43 am
At //5 only the local variable obj is set to null but the original object created at //1 is still being referred to by the instance variable o of tc (Notice the call tc.doSomething(obj); which sets tc.o to point to the same object). This instance variable is changed to point to another object at //6. So it is only after //6 that the original object created at //1 is not being pointed to by any reference.

HTH,
Paul.
What is here the local variable obj? The one created at line //4 ?