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