Junior — Senior
Analysis of argument passing in Java method
livecode
Task condition
Rewrite the code considering the features of passing parameters by value and by reference. Describe what happens when variables z and a are passed to the method doIt(). What values will a, a.instanceNumber, a.x, a.y and z have after the method completes? What is the effect of the line a = new A(2); inside the method and why does it behave this way?
public class A {
private static int y;
private final int instanceNumber;
private int x;
public A(int instanceNumber) {
this.instanceNumber = instanceNumber;
}
public String toString() {
return String.valueOf(instanceNumber);
}
private static void doIt(int i, A a) {
i = 11;
a.x = 12;
y = 13;
a = new A(2); // ?
a.x = 21;
y = 22;
}
public static void main(String[] args) {
A a = new A(1); // 1
a.x = 1;
y = 2;
int z = 3;
A.doIt(z, a);
}
}