public class Test2 {
public static void main(String[] args) {
Test2 obj=new Test2();
String a=obj.go();
System.out.print(a);
}
Try using StringBuffer instead of String and you will see the change .... it seems the return statement blocks the object which is to be returned and not the reference. You could also try to verify this by printing the hashcode of :
object being printed from main()
public static void main(String[] args){
Test obj=new Test();
StringBuffer a=obj.go();
System.out.print(a);
}
public StringBuffer go() {
StringBuffer q=new StringBuffer("hii");
try {
return q;
}
finally {
q=q.append("hello");
System.out.println("finally value of q is "+q);
}
}