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