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