Given the following code:
public class Intmat {
private static int enso = 0;
private int momp = 0;
public void aron(int sose) {
A
int wo = 0;
enso += sose;
wo += sose;
momp += sose;
System.out.println("enso=" + enso + " wo=" + wo + " momp=" + momp);
}
public static void main(String[] args) {
B
Intmat i0 = new Intmat();
Intmat i1 = new Intmat();
i0.aron(1);
i1 = i0;
i1.aron(10);
i0.aron(100);
i0 = i1;
i1.aron(1000);
C
}
}
momp, enso, wo, i0, i1] are in scope at A ?Output:
momp=1 enso=1 wo=1 momp=11 enso=10 wo=11 momp=111 enso=100 wo=111 momp=1111 enso=1000 wo=1111
In scope at A : momp, wo, enso
In scope at B : momp, i0
In scope at C : momp
Explanation (which you do not need to write out in your submitted solution):
momp is a static variable, wo is an instance variable, and enso is a local variable.
At A , 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. wo is out of scope because it is an instance variable, but main is a static method. enso is out of scope because it is local to aron.
At C , i0 and i1 are out of scope because they are not declared yet. wo is out of scope because it is an instance variable, but main is a static method. enso is out of scope because it is local to aron.
Related puzzles: