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