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