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