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