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