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