} OUTPUT : ar1:EARTH ar2:MOON why only ar1 is reflecting not ar2 .In java objects are always passed by reference(passing the copy of reference variable).same hare I have passed the copy of reference ar1 & ar2.In method m1() arg2=arg1.It should reflect in main also.
objects are passed by reference in the sense that the reference variable is copied so that you can never change the original reference variable but you can change the object(if it is not immutable) by accessing it through the copy of reference variable.
At 1 in above code, reference arg1's value has been copied to reference variable arg2. So, now arg2 is also pointing to the same object as arg1. But ar2 reference variable in main method is still pointing to the old ar2 object.
Now at 2 in your code, ar2.getStr() will still refer to the actual ar2 object.
insert one line to your code and you will see the difference. Replace m1 method in your code with method below,
hi all, thanks for quick response Sarah I have already tested the changes before posted, which you have done. Mr joshi answer is quit clear that during passing the object ,the reference variable is copied to the called method.So the original reference variable can never be modify.Although using the copied reference we can modify the object state.
Thanks all of you
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?