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