TestObj class instance t invokes first() method which in turn calls second() method. Here we craete another object of Value val and v is a reference variable pointing to val and thereby accessing v.i results in acessing the value of val.i which is equal to 15 and i is the local variable which will be accessed not the argument passed. And the print in first method will give v.i=25 and local value of i=5.
at line 2 you are passing the reference to an object created at line 1 and the local variable of method first(), pay attention that you are calling second(v,i) and not second(v,v.i)..
now in the second() you chnage the value of i, which is local to second() from 5 to 0.but because we use pass by value it won't be reflected back in first().
at line 4 you change the state of object reffered to by variable v in second(), which now will be reflected back to first().
at line 5 you create a new object refrrefd by val and at line 6 make 'v' which is local to second() also refer to it.but it wont be reflected back to variable 'v' in first().which mean 'v' in first() will still be referring to object created at line 1.
at line 6 you print v.i which is equivalent to val.i so 15 gets printed and you also print 'i' which is the local variable of second() and you already change it's value from 5 to 0.hence 0 also gets printed.
so after first print statement you get:: 15 0
now the controls get back to first().
because you changed the state of object referred to by 'v' at line 1 from 25 to 20 at line 4(which is reflected back..) v.i at line 3 prints 20. also 'i' that you printe at line 3 is local to first() whose value is 5
so 5 gets printed so you get:: 20 5 as next o/p.
"History would be kind to me, for I intend to write it."
Hey Divya Can any one explain me below program, the value of v.i. how we rae getting?
When you are passing object of value to method second() then you passing the reference to the original object and i is the copy that is because it is primitive and other is object . but in method second when you call Value val = new Value(); v= val then v is now pointing to another object of Value type so what ever modifications done on v now will not be shown in the method first.
That is the reason the value of object is modified when you call v.i=2- before Value val = new Value(); v= val and value of i will not changes until explicitly value is returned to first and assigned to i .
I spent the whole morning looking at this code and was trying to understand Priyam's explanation, which is correct, but was still not getting it. I just read this article that explains it sooo well and in pictures :-) which I prefer way more. It is a must read, please find it on http://www.javaranch.com/campfire/StoryPassBy.jsp
I am new to Java and this question has really cleared up a major doubt I had in Java's fundamentals. Nice query you posted...thanks. :-) [ August 02, 2007: Message edited by: Meghna Bhardwaj ]
We find this kind of rampant individuality very disturbing. But not this tiny ad: