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