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