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