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