could someone explain this piece of code. The output is where I am confused. It seems only one thing (i.e. p) was modified by the modifyObject function, but the other (i.e. s) is left unchanged. But I am confused. Could someone explains what's going on.
class Person{
int a = 8;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
@Override
public String toString() {
return "Person [a=" + a + "]";
}
}
public class TestMutable {
public static void main(String[] args)
{
Person p = new Person();
p.setA(34);
String s = "bar";
modifyObject(s, p); //Call to modify objects
System.out.println(s);
System.out.println(p);
}
private static void modifyObject(String str, Person p)
{
str = "foo";
p.setA(45);
}
}
why is it that the following is output? i.e. str is still bar , but person.A is now 45?
bar
Person [a=45]