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