At Line 3 v is pointing to the object having Hascode Value@123ab
and after that(at Line 5) the v is pointing to the new object having Hascode Value@456cd
and hense the output at Line 6 is 15 0
So my question is that why ouptput is not
As we are assining the new object to the reference at Line 5 after returnig from second()
the value reference must points to the object created at Line 4(Hashcode Value@456cd) and not to the object created at Line 1( Hascode Value@123ab)
The important thing is not to stop questioning.Curiosity has its own reason for existing.
in Line 3 you set the value of v.i to 20. Then you use the reference value to point to a completely different object, created in Line 4 with the value of int i = 15
When reassigning v in the method second, the reference variable v in method first is unaffected since its method local.
Key word : Pass by Value ! not by Reference
what i mean is: the Value (an Object in this case) is passed to the second method, so changes affect the actual object. However after the reassigning to the new Object this isnt the case anymore