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