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