• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need clarification for Question 10, Chapter 3 from book OCAJP7 K & B

 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few doubts in the below code.
Could someone explain me.


1. p = this;
    what is this referring to in this statement. I know that 'this' referes to current instance, but this statement is confusing.


Is the above arg constructor is similar to the below mentioned one?

Network(int x, Network n){
id = x;
p = n;
if(n!=null)
p=n;

}

2. System.out.println(n3.p.p.id);

   How does the above statement work ?

Thanks in advace :-)
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Indu,

1. p = this;
    what is this referring to in this statement.

Here this refers to the object created at line 15 for the first object creation. To understand it , Look at the below sample code .


The above code outputs


Please observe the output . When you print reference variables p , n1 , the output is same . I mean hashcode is same and also p.id , n1.id giving same value in the output. That is the proof that both p, n1 are referering that same object  you created first. Note that when you print reference variable n , the output is null, so p , n are different now.

Next Look at the another version of code




It outputs like
com.mss.Network@64a294a6 1
false
com.mss.Network@64a294a6 1
com.mss.Network@3b95a09c 2
true
com.mss.Network@3b95a09c 2
com.mss.Network@6ae40994 3
true
com.mss.Network@6ae40994 3


Please observe the output. From the output , my conclusions are

1.  this always refers to the latest created object.
2. From second object creation on wards p, n are same . because constructor second  parameter is not null from second object creation on wards.

2. System.out.println(n3.p.p.id);

   How does the above statement work ? 


Based on above program output , you can debug the this statement. n3.p gives reference(access) to n2 reference  variable's object. n3.p.p gives reference(access) to n1 reference variable's object. Now You have reference to access instance variables(id) values in first object you created. n3.p.p.id prints  id value in first object you created.

Please correct me if anything wrong.

Hope this helps !








 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Indu Acharya,

First of all, a warm welcome to CodeRanch!

Indu Acharya wrote:1. p = this;
    what is this referring to in this statement. I know that 'this' referes to current instance, but this statement is confusing.

2. System.out.println(n3.p.p.id);

   How does the above statement work ?

Have a look at this topic! It explains the same mock question and exactly the same questions as yours are answered in this topic. So definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags while posting code.

Indu Acharya wrote:p = this;
    what is this referring to in this statement. I know that 'this' referes to current instance, but this statement is confusing.
When you create an object of Network class using new operator that invokes the respective constructor, in that constructor this keyword refers to the object which caused invocation of that constructor i.e. current object

Is the above arg constructor is similar to the below mentioned one?
Network(int x, Network n){
id = x;
p = n;
if(n!=null)
p=n;

}
No, here reference variable n of type Network is being assigned to p where n can be referring to any object of type Network passed as an argument to the constructor.

But in your example p = this; means reference of current object is assigned.

System.out.println(n3.p.p.id);
How does the above statement work ? 


1. Each object of Network class you create will have separate instance variable id of type int and p of type Network.
It creates an object of class Network, invokes constructor and passes 1 and null as arguments
where in constructor x is assigned to id means id = 1 and p = this; means p refers to the current object means object referred by n1. We know n is null so if (n != null) is false p = n; is not executed.

Now we have n1.id = 1 and n1.p refers to the same object which n1 refers i.e. current object

2. Here in below code, you are passing copy of reference of an object referred by n1.


3. In method go
Creates an object of Network, invokes constructor and passes 2 and copy of reference of an object referred by n1 as arguments.

In constructor x is assigned to id means id = 2 and here p refers to the object referred by n1 because this time if (n != null) is true so p = n; is executed.

Now we have n2.id = 2 and n2.p refers to the same object which n1 refers.

4. On next line in method go we have
Creates an object of Network, invokes constructor and passes 3 and copy of reference of an object referred by n2 as arguments.

In constructor x is assigned to id means id = 3 and here p refers to the object referred by n2 because this time if (n != null) is true so p = n; is executed.

Now we have n3.id = 3 and n3.p refers to the same object which n2 refers.


5.
Lets start from left side of n3.p.p.id
n3.p refers to the same object which n2 refers see point 4 so n3.p means n2
Replace n3.p with n2 now we have

n2.p.id but we know n2.p refers to the same object which n1 refers, see point 3 so n2.p means n1
Replace n2.p with n1 now we have

n1.id whose value is 1, see point 1

Finally It prints 1

In pictorial form It would be easy to understand:



Diagram-.png
[Thumbnail for Diagram-.png]
 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Narayana Bojja wrote:Hi Indu,

1. p = this;
    what is this referring to in this statement.

Here this refers to the object created at line 15 for the first object creation. To understand it , Look at the below sample code .


The above code outputs


Please observe the output . When you print reference variables p , n1 , the output is same . I mean hashcode is same and also p.id , n1.id giving same value in the output. That is the proof that both p, n1 are referering that same object  you created first. Note that when you print reference variable n , the output is null, so p , n are different now.

Next Look at the another version of code




It outputs like
com.mss.Network@64a294a6 1
false
com.mss.Network@64a294a6 1
com.mss.Network@3b95a09c 2
true
com.mss.Network@3b95a09c 2
com.mss.Network@6ae40994 3
true
com.mss.Network@6ae40994 3


Please observe the output. From the output , my conclusions are

1.  this always refers to the latest created object.
2. From second object creation on wards p, n are same . because constructor second  parameter is not null from second object creation on wards.



Very well explained Narayana. Println statements help me understand better and they definitely help me understanding other questions as well.

2. System.out.println(n3.p.p.id);

   How does the above statement work ? 


Based on above program output , you can debug the this statement. n3.p gives reference(access) to n2 reference  variable's object. n3.p.p gives reference(access) to n1 reference variable's object. Now You have reference to access instance variables(id) values in first object you created. n3.p.p.id prints  id value in first object you created.




I got it now

n3.p is n2
n2.p is n1
n1.id is 1


hence  n3.p.p.id is 1


Please correct me if anything wrong.

Hope this helps !


I do not see anything wrong in your explanation. It definitely helps.
Thanks :-)







 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Have a look at this topic! It explains the same mock question and exactly the same questions as yours are answered in this topic. So definitely worth reading!




Hi Roel,

I referred your post. It was very helpful, while refering to the above question I found answer to the other question (qn 11,chapter 3) as well.
I have one doubt in that, I will post my question in that thread.

 
Indu Acharya
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags while posting code.


I did use code tags, may be I didn't use it in proper way. Posting code for the first time.



When you create an object of Network class using new operator that invokes the respective constructor, in that constructor this keyword refers to the object which caused invocation of that constructor i.e. current object


Got it. this refers to the current object.
when the first object n1 is created it referes to n1. After n2 invocation it refers to n2 and so on



1. Each object of Network class you create will have separate instance variable id of type int and p of type Network.
It creates an object of class Network, invokes constructor and passes 1 and null as arguments
where in constructor x is assigned to id means id = 1 and p = this; means p refers to the current object means object referred by n1. We know n is null so if (n != null) is false p = n; is not executed.

Now we have n1.id = 1 and n1.p refers to the same object which n1 refers i.e. current object

2. Here in below code, you are passing copy of reference of an object referred by n1.


3. In method go
Creates an object of Network, invokes constructor and passes 2 and copy of reference of an object referred by n1 as arguments.

In constructor x is assigned to id means id = 2 and here p refers to the object referred by n1 because this time if (n != null) is true so p = n; is executed.

Now we have n2.id = 2 and n2.p refers to the same object which n1 refers.

4. On next line in method go we have
Creates an object of Network, invokes constructor and passes 3 and copy of reference of an object referred by n2 as arguments.

In constructor x is assigned to id means id = 3 and here p refers to the object referred by n2 because this time if (n != null) is true so p = n; is executed.

Now we have n3.id = 3 and n3.p refers to the same object which n2 refers.


5.
Lets start from left side of n3.p.p.id
n3.p refers to the same object which n2 refers see point 4 so n3.p means n2
Replace n3.p with n2 now we have

n2.p.id but we know n2.p refers to the same object which n1 refers, see point 3 so n2.p means n1
Replace n2.p with n1 now we have

n1.id whose value is 1, see point 1

Finally It prints 1

In pictorial form It would be easy to understand:







You have explained each and every step in detail. It helped me understand the code better.
and special thanks for explaining in pictorial form, best way to remember.
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indu Acharya wrote:I did use code tags, may be I didn't use it in proper way. Posting code for the first time.
Eh! Sorry, I think my mind was too busy   you had used but not properly. Of course not expected from someone who is posting for the first time.

[code tag]Your code comes here[/code tag] You can preview to verify It. You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic