this code segment is an alteration to a question in the oca 7 study guide. dealing with array and arrayList.
I am trying to wrap my head around how the object Box b1 is altered by the go method.
class Box {
int size;
Box (int s){size = s;}
}
public class Laser {
public static void main(String[] args) {
Box b1 = new Box(5);
System.out.println("this is b1 at line 20" + " = " +b1.size);
Box[] ba = go(b1, new Box(6));
System.out.println("this is b1 at line 22" + " = " +b1.size);
ba[0] = b1;
for(Box b : ba) System.out.print(b.size + " ");
}
static Box [] go(Box b3, Box b4){
b3.size=4;
Box[] ma = {b4,b3};
return ma;
}
}
The output is
this is b1 at line 20 = 5
this is b1 at line 22 = 4
4 4
I expected the output to be
this is b1 at line 20 = 5
this is b1 at line 22 = 5
5 4