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