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