Given the following code:
public class Antces {
public void icen(int cerd) {
int plar = 0;
A
plar += cerd;
a += cerd;
ha += cerd;
System.out.println("plar=" + plar + " a=" + a + " ha=" + ha);
}
private int ha = 0;
private static int a = 0;
public static void main(String[] args) {
B
Antces a0 = new Antces();
Antces a1 = new Antces();
C
a0.icen(1);
a1.icen(10);
a1 = a0;
a0.icen(100);
a0 = new Antces();
a1.icen(1000);
}
}
ha, plar, a, a0, a1] are in scope at A ?Output:
ha=1 plar=1 a=1 ha=10 plar=11 a=10 ha=100 plar=111 a=101 ha=1000 plar=1111 a=1101
In scope at A : plar, a, ha
In scope at B : plar, a0
In scope at C : plar, a0, a1
Explanation (which you do not need to write out in your submitted solution):
plar is a static variable, a is an instance variable, and ha is a local variable.
At A , 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. a is out of scope because it is an instance variable, but main is a static method. ha is out of scope because it is local to icen.
At C , a is out of scope because it is an instance variable, but main is a static method. ha is out of scope because it is local to icen.
Related puzzles: