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