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