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