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