I am writing a simple application like bellow:
public class Test1 { String name; public Test1(String name) { this.name=name; } public void setName(String name) { this.name = name; } public String getName() { return name; } @Override public String toString() { return name; } } And
public class Test2 { /** * @param args */ public static void main(String[] args) { for(int i=0;i<10;i++) { if(i>6)break; } Test2 test2= new Test2(); Test1 test1= new Test1("Test1"); test2.doTest(test1); System.out.println(test1); } public void doTest(Test1 test1) { test1= new Test1("Test2"); } } As I am passing test1 object reference to doTest and reassigning a new value to that object, I believe the output of the avobe program will be Test2. But it gives as Test1 only. It seems the new object created inside doPost is local to that method and does not effect outside. But as Here I am passing the reference to the object, why the value of the object will not be changed to Test2 still?