Pass-by-reference is giving me a bit of a headache, so I'd like to see how confusing the rest of the community would find this question. Please feel free to say if you think it's a stupid question.
What would you say is the output of the following program?
class Demo {
public static void main(String[] args) {
ArrayList<Container> list = new ArrayList<Container>();
Container c1 = new Container();
Container c2 = new Container();
c1.setSt("c1");
c2.setSt("c2");
list.add(c1);
list.add(c2);
Container c3 = new Container();
c3.setSt("c3");
modify(list, c3);
for (Container st : list) {
System.out.println(st.getSt());
}
System.out.println("c3.st: " + c3.getSt());
}
private static void modify(ArrayList<Container> list, Container container) {
list.get(1).setSt("modified");
container.setSt("modified c3");
container = new Container();
container.setSt("new container");
}
}
class Container {
private String st;
public String getSt() {
return st;
}
public void setSt(String st) {
this.st = st;
}
}