Given the following code:
public class Groffai {
public void ochou(int ifer) {
A
int aist = 0;
naro += ifer;
a += ifer;
aist += ifer;
System.out.println("naro=" + naro + " a=" + a + " aist=" + aist);
}
private static int a = 0;
public static void main(String[] args) {
B
Groffai g0 = new Groffai();
Groffai g1 = new Groffai();
C
g0.ochou(1);
g1.ochou(10);
g0.ochou(100);
g1 = g0;
g0 = new Groffai();
g1.ochou(1000);
}
private int naro = 0;
}
aist, naro, a, g0, g1] are in scope at A ?Output:
aist=1 naro=1 a=1 aist=10 naro=11 a=10 aist=101 naro=111 a=100 aist=1101 naro=1111 a=1000
In scope at A : naro, aist, a
In scope at B : naro, g0
In scope at C : naro, g0, g1
Explanation (which you do not need to write out in your submitted solution):
naro is a static variable, aist is an instance variable, and a 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. aist is out of scope because it is an instance variable, but main is a static method. a is out of scope because it is local to ochou.
At C , aist is out of scope because it is an instance variable, but main is a static method. a is out of scope because it is local to ochou.
Related puzzles: