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