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