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