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