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