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