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