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