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