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