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