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