Originally posted by imran anwar:
can you tell me how different the situation of b is than a? what i am saying is, if 'a' contains reference of 'exactly' that object which was created in main method... will b also not contain its corresponding reference created in main?
Imran,
The key to understanding this is to see that the variable b in main and the paramater b in the method are two distinct variables. They are both reference variables, which reference a StringBuffer object. When swap is called, both b in main and b in swap contain the same value (a copy of b in main is sent to the method swap). Therefore, both reference variables reference the same object. However, in the last line of the swap method, we're changing the variable b in the swap method. Note that we're not making any sort of modification to the object referenced by b, we're just changing what it references. Therefore, after that line is executed, the variable b in swap references a
different object than the variable b in main.
Take a look at the Flash app that I linked to in the earlier post - hopefully, it'll help clear this up for you.
I hope that helps,
Corey