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