Given the following code:
public class Ilfro {
private int lo = 0;
public void stri(int za) {
int ze = 0;
lo += za;
cac += za;
ze += za;
System.out.println("lo=" + lo + " cac=" + cac + " ze=" + ze);
A
}
public static void main(String[] args) {
B
Ilfro i0 = new Ilfro();
Ilfro i1 = new Ilfro();
C
i0.stri(1);
i1.stri(10);
i0 = i1;
i1 = i0;
i0.stri(100);
i1.stri(1000);
}
private static int cac = 0;
}
ze, lo, cac, i0, i1] are in scope at A ?Output:
ze=1 lo=1 cac=1 ze=10 lo=11 cac=10 ze=110 lo=111 cac=100 ze=1110 lo=1111 cac=1000
In scope at A : lo, ze
In scope at B : lo, i0
In scope at C : lo, i0, i1
Explanation (which you do not need to write out in your submitted solution):
lo is a static variable, ze is an instance variable, and cac is a local variable.
At A , cac is out of scope because it is not declared yet. i0 and i1 out of scope because they are local to the main method.
At B , i1 is out of scope because it is not declared yet. ze is out of scope because it is an instance variable, but main is a static method. cac is out of scope because it is local to stri.
At C , ze is out of scope because it is an instance variable, but main is a static method. cac is out of scope because it is local to stri.
Related puzzles: