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