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