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