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