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