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