Hi, Can anyone please help me in clearing the doubt I have with the Object reference in the following program.The output of this is 10 0 20.But when I walk through the program I am getting 10 0 10.the reason I think is after the statement v=vh;v is pointing to the object with the value of i being 10.In that case the final output will be 10 0 10 why am I getting 10 0 20.I am confused can some one help me in this regard.thanks in advance. kamesh. here is the code from Marcus Mock exam 2: following code what will be the output?
I added UBB code here - Mapraputa [This message has been edited by Mapraputa Is (edited October 20, 2000).]
Hi Mr Kamesh.Let's walk through the program once again. 1) ValHold v = new ValHold(); v.i=30; another(v,i);
This code creates a obj. reference v and passes that reference to the method another().Inside another we have a LOCAL VARIABLE v.This variable also points to the same obj. v created in method amethod().So now we have TWO REFERENCES pointing to same obj. 2)Inside method another , v.i=20; is execeuted.This means that value of instance variable i of obj. v is changed from 30 to 20. 3)Now a new obj. is created referenced by LOCAL VARIABLE vh.The lOCAL VARIABLE v is assigned this value.Thus LOCAL VAR. v is detached from the original object it was pointing to and now LOCAL VAR. v and vh point to same object.Since vh is a new obj. instance var vh.i = 10 and HENCE v.i=10. LOCAL VARIABLE i is assigned value 0 on entry to method another() so printing these values gives 10 and 0 respectively. 3)The original object ref v is unchanged , it is still pointing to the object created in aMethod().Instance variable. i value was changed to 20 in another(),so this prints 20. Thus final answer is 10 0 20. I hope i have been clear enough. ------------------ Come on in !! Drinks are on the house in the Big Moose Saloon !!
Udayan Naik<BR>Sun Certified Programmer for the Java 2 Platform
When the 'another' method is called from the method 'amethod' with passing the variable 'v', only the copy of the value stored in that variable (which is a reference to an object) is passed. Which means after the method another is called it will have a variable 'v' different from the variable 'v' of the calling method (amethod) but having the same value as that of the variable 'v' of the calling method. Which means both variables hold references which points to the same object. So v.i = 20 statement in another affects the original object because v in both methods (before v=vh) refer only one object. Now in another method, after the statement v=vh, v refer a new object but it does not change the value of v in the calling method thus v in the calling method still holds the original reference and not a new reference to the object created in the method - another.
Joined: Oct 17, 2000
Thanks Uday and Gautam.But I am still confused when we are able to change the value in the another() from v.i=30 to v.i=20 what i could understand is that the objects are passed by reference.Uday when you say that local variable of another() v is also pointing to the same object ie the object created in amethod() I am not able to get that (can you please tell me which line of the code shows that)Actually I am getting confused.I think my Funda is going wrong.Can anyone suggest a book which clears the passing by refernece or passing by copy of the reference(as used by Gautam). I would appreciate for the help.I know these are the basic things but still i am not confident. kamesh
Hi Kamesh, I've added comments to your example to explain what's happening.
The thing to remember is that Java passes everything as a copy of the original value. If you pass a primitive type; a copy of that type and value is created and given to the method. If you pass an object reference, a copy of the object reference is given to the method. A copy of an object reference still points to the original object. Which is why v.i = 20 changes the value in the ValHold object used by aMethod. When you created vh you created a new ValHold object. Assigning vh to v changed the value of the object reference in the method another BUT has no effect on the original object reference created in aMethod. Hope that helps. ------------------ Jane
Jane, Thanks a lot and also thanks once again to UDay and Gautam.Can someone suggest me a site where I can practice this type of problems so that i my self become familiar with passing parameters by value and reference.Thanks in advance. kamesh.