Given the following code:
public class Inshic {
public void shiar(int caia) {
A
int ried = 0;
ried += caia;
il += caia;
a += caia;
System.out.println("ried=" + ried + " il=" + il + " a=" + a);
}
private static int a = 0;
public static void main(String[] args) {
Inshic i0 = new Inshic();
B
Inshic i1 = new Inshic();
i0.shiar(1);
i1 = i0;
i1.shiar(10);
i0 = new Inshic();
i0.shiar(100);
i1.shiar(1000);
C
}
private int il = 0;
}
a, ried, il, i0, i1] are in scope at A ?Output:
a=1 ried=1 il=1 a=10 ried=11 il=11 a=100 ried=100 il=111 a=1000 ried=1011 il=1111
In scope at A : il, ried, a
In scope at B : il, i0, i1
In scope at C : il
Explanation (which you do not need to write out in your submitted solution):
il is a static variable, ried is an instance variable, and a is a local variable.
At A , i0 and i1 out of scope because they are local to the main method.
At B , ried 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 shiar.
At C , i0 and i1 are out of scope because they are not declared yet. ried 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 shiar.
Related puzzles: