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