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