Given the following code:
public class Shissbi {
private int id = 0;
public void ionad(int es) {
int o = 0;
id += es;
o += es;
heus += es;
System.out.println("id=" + id + " o=" + o + " heus=" + heus);
A
}
public static void main(String[] args) {
B
Shissbi s0 = new Shissbi();
Shissbi s1 = new Shissbi();
C
s0.ionad(1);
s1.ionad(10);
s0 = new Shissbi();
s1 = new Shissbi();
s0.ionad(100);
s1.ionad(1000);
}
private static int heus = 0;
}
heus, id, o, s0, s1] are in scope at A ?Output:
heus=1 id=1 o=1 heus=10 id=10 o=11 heus=100 id=100 o=111 heus=1000 id=1000 o=1111
In scope at A : o, heus
In scope at B : o, s0
In scope at C : o, s0, s1
Explanation (which you do not need to write out in your submitted solution):
o is a static variable, heus is an instance variable, and id is a local variable.
At A , id is out of scope because it is not declared yet. 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. heus is out of scope because it is an instance variable, but main is a static method. id is out of scope because it is local to ionad.
At C , heus is out of scope because it is an instance variable, but main is a static method. id is out of scope because it is local to ionad.
Related puzzles: