Given the following code:
public class Pipo {
public static void main(String[] args) {
Pipo p0 = new Pipo();
A
Pipo p1 = new Pipo();
p0.tamu(1);
p1 = p0;
p0 = new Pipo();
p1.tamu(10);
p0.tamu(100);
p1.tamu(1000);
B
}
private int eu = 0;
private static int es = 0;
public void tamu(int ti) {
int nes = 0;
es += ti;
nes += ti;
eu += ti;
System.out.println("es=" + es + " nes=" + nes + " eu=" + eu);
C
}
}
eu, es, nes, p0, p1] are in scope at A ?Output:
eu=1 es=1 nes=1 eu=11 es=10 nes=11 eu=111 es=100 nes=100 eu=1111 es=1000 nes=1011
In scope at A : eu, p0, p1
In scope at B : eu
In scope at C : eu, nes
Explanation (which you do not need to write out in your submitted solution):
eu is a static variable, nes is an instance variable, and es is a local variable.
At A , nes is out of scope because it is an instance variable, but main is a static method. es is out of scope because it is local to tamu.
At B , p0 and p1 are out of scope because they are not declared yet. nes is out of scope because it is an instance variable, but main is a static method. es is out of scope because it is local to tamu.
At C , es 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: