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