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