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