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