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