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