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