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