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