Given the following code:
public class Huhtess {
private static int di = 0;
public static void main(String[] args) {
Huhtess h0 = new Huhtess();
A
Huhtess h1 = new Huhtess();
B
h0.nocNawhol(1);
h1 = new Huhtess();
h0 = h1;
h1.nocNawhol(10);
h0.nocNawhol(100);
h1.nocNawhol(1000);
}
private int ec = 0;
public void nocNawhol(int mi) {
int poal = 0;
C
poal += mi;
di += mi;
ec += mi;
System.out.println("poal=" + poal + " di=" + di + " ec=" + ec);
}
}
ec, poal, di, h0, h1] are in scope at A ?Output:
ec=1 poal=1 di=1 ec=10 poal=11 di=10 ec=100 poal=111 di=110 ec=1000 poal=1111 di=1110
In scope at A : poal, h0, h1
In scope at B : poal, h0, h1
In scope at C : poal, di, ec
Explanation (which you do not need to write out in your submitted solution):
poal is a static variable, di is an instance variable, and ec is a local variable.
At A , di is out of scope because it is an instance variable, but main is a static method. ec is out of scope because it is local to nocNawhol.
At B , di is out of scope because it is an instance variable, but main is a static method. ec is out of scope because it is local to nocNawhol.
At C , h0 and h1 out of scope because they are local to the main method.
Related puzzles: