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