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