Possible Duplicate:
Is Java pass by reference?
How come this code of mine is not working? I am passing an object into a method but it won’t modify the original object that I created in main, why is that?
public class TestRun{
public static void main(String[] args){
Test t1 = new Test();
Test t2 = new Test();
t2.x = 555;
t2.y = 333;
System.out.println("The original valuess of X and Y are");
System.out.println("X = "+t1.x+"Y = "+t1.y);
modifyObject(t1,t2);
System.out.println("After the modification ");
System.out.println("X = "+t1.x+"Y = "+t1.y);
}
public static void modifyObject(Test arg1,Test arg2){
arg1 = arg2;
}
}
public class Test{
int x = 9999;
int y = 1;
}
You answered it in the title. Java is “pass by value”.
This means that the method receives only copies of the object references
arg1andarg2.You can, however, alter the contents of the objects, by doing