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

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

Moderator: admin

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

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

Post by admin »

There is only one variable named obj in the given code and that variable is declared at line //1. So, I am not sure what is the confusion about the variable.
Please remember that variable and the object to which it points are two different things.
If you like our products and services, please help us by posting your review here.

morarumaria1988
Posts: 5
Joined: Mon Oct 30, 2017 5:17 am
Contact:

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

Post by morarumaria1988 »

It's clear now, thanks!

demetrio
Posts: 16
Joined: Mon Sep 30, 2019 11:40 am
Contact:

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

Post by demetrio »

Suppose the question code was (I just added one println line):

public class NewClass {
private Object o;
void doSomething(Object s){
o = s;
//line bellow was added
System.out.println("the second time this line is printed is the exact moment "
+ "when the object created in line 1 can be Garbage Collected"); //line 7
}

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
}
}

Am I right? I mean, the second time line 7 is evoked is the exact moment where the object created in line 1 is eligible for garbage collector, right?

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

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

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

qazwsx922
Posts: 7
Joined: Sat Sep 04, 2021 8:17 pm
Contact:

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

Post by qazwsx922 »

How do I know line5 makes obj(on line 4) null?,instead of line 1 obj?

I thought line 5 made line1 obj null.

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

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

Post by admin »

The code given in the question is:

Code: Select all

      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
obj is being reassigned a new Object at //4, that means the statement obj = null; at //5 will cause that Object to lose the reference.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 36 guests