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