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