class Value { public int i = 15; } //Value public class Test { public static void main(String argv[]) { Test t = new Test(); t.first(); } public void first() { int i = 5; Value v = new Value(); v.i = 25; second(v, i); System.out.println(v.i); } public void second(Value v, int i) { i = 0; v.i = 20; Value val = new Value(); v = val; System.out.println(v.i + " " + i); } } // Test
Objects in Java are always passed through the method calls "BY VALUE" and never "BY REFERENCE".
Now, lets go through the code.
After the t object is created, it calls the method first(). Here object v gets defined, with values as -- v.i =25 Then a call to the second() method is made. Here, a copy of the object reference is sent to the second method. That is, if v in first is pointing to some object, the copy of that object address is passed to second method. Lets take that address to be obj123. So, the value of v passed -- obj123
IN SECOND METHOD
Value received in second method v = obj123. v.i changed from 25 to 20
In second method, another new Value object val is created. Lets take that val conatins -- obj456 Now, val.i = 20
After v = val
v (in second method ) = obj456
Therefore, in second method print statement, 15 and 0 are printed.
IN FIRST MEHOD Now, when it goes back to the first method, the value of v is = obj123, beacuse, you have just passed the copy of the address of the object that v was pointing out to and not the object itself. Even if that value of the copy was changed in the called method (second method) , it wont affect the original copy of the object address in the calling method. So, the value of v.i = 20 is printed.
i think if you do not repeat the variable names it will be lot clearer.
like you said the output would be 15 0 20
Value v = new Value();v.i = 25; // create a new Value object with referene v and memeber 25.
second(v, i);// calling second method with v and i from above as parameters.
in the second() method you take these parameters as v and i. now these are new references visible only in the second() method. however with this new v reference you could still modify the referenced object member variable like i (which is what you do by saying v.i = 20). then you make a new Value object and have the v reference ( in the seond() method) to point to this new object. which is why it has memeber value 15. so it prints 15 and 0.
now control comes back to first() method. and it proints 20 coz u have changed the object.
rememebr when going between methods the object remains the same, but the refereences change (pass by 'copy').
Hi Naresh, Java always passes parameters by value and not by reference. If they are primities then a copy of the value is passed. If it is an object(reference), a copy of the reference is passed. Because the copy also refers to the original object, the object referred by the copy(method parameter) changes.
Post by:autobot
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking