Given the following code:
public class Swel {
public void caeo(int e) {
A
int sirm = 0;
gri += e;
sirm += e;
gac += e;
System.out.println("gri=" + gri + " sirm=" + sirm + " gac=" + gac);
}
private static int gri = 0;
public static void main(String[] args) {
B
Swel s0 = new Swel();
Swel s1 = new Swel();
s0.caeo(1);
s1.caeo(10);
s1 = s0;
s0 = s1;
s0.caeo(100);
s1.caeo(1000);
C
}
private int gac = 0;
}
gac, gri, sirm, s0, s1] are in scope at A ?Output:
gac=1 gri=1 sirm=1 gac=11 gri=10 sirm=10 gac=111 gri=100 sirm=101 gac=1111 gri=1000 sirm=1101
In scope at A : gac, sirm, gri
In scope at B : gac, s0
In scope at C : gac
Explanation (which you do not need to write out in your submitted solution):
gac is a static variable, sirm is an instance variable, and gri is a local variable.
At A , s0 and s1 out of scope because they are local to the main method.
At B , s1 is out of scope because it is not declared yet. sirm is out of scope because it is an instance variable, but main is a static method. gri is out of scope because it is local to caeo.
At C , s0 and s1 are out of scope because they are not declared yet. sirm is out of scope because it is an instance variable, but main is a static method. gri is out of scope because it is local to caeo.
Related puzzles: