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