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