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