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