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