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