I must admit I was schocked when sb=null[I had bet my money here] was actually sb=bbbaaabbb in the final output. why setting the local reference of sb (in method testRefs()) to null, does not affect sb . ....? **PS( b.t.w output of 's' is all fine......it's only 'sb' that's bothering)
When you pass an object reference to a method, only the copy of the reference variable is passed. Then both the variable and its copy refer to the same object. So both the variables - sb in the main method and sb in the testRef method - refer to the same StringBuffer object. So the object can be modified by using either of these variables.
However, making the variable sb in the testRef method refer to null, will not affect the variable sb in the main method. It will still refer to the same StringBuffer object itself which now contains the value "bbbaaabbb"
Following on from Seema says, if you change the name of the parameter in method testRefs from sb to aStringBuffer, and change all of the usages of sb in the body of method testRefs to aStringBuffer, then you will see clearer what is going on: