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