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