1. p = this;
what is this referring to in this statement.
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
2. System.out.println(n3.p.p.id);
How does the above statement work ?
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 ?
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 objectIndu 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.
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.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;
}
System.out.println(n3.p.p.id);
How does the above statement work ?
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 :-)
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!
Please UseCodeTags while posting code.
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
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:
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.Indu Acharya wrote:I did use code tags, may be I didn't use it in proper way. Posting code for the first time.
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|