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