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