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