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