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