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