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