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