Code: Select all
public class TestClass{
public void testRefs(String str, StringBuilder sb){
str = str + sb.toString();
sb.append(str);
str = null;
sb = null;
[b] System.out.println("s="+str+" sb="+sb);// prints out s=null and sb=null[/b]
}
public static void main(String[] args){
String s = "aaa";
StringBuilder sb = new StringBuilder("bbb");
new TestClass().testRefs(s, sb);
System.out.println("s="+s+" sb="+sb);
}
}
s=aaa sb=null
where as the correct answer is s=aaa sb=bbbaaabbb
Its a great question and wondering if you can point me in the direction of some text explaining why Setting the local reference str and sb (in method testRefs()) to null, does not affect the variables s and sb of the main() method
Thanks