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