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