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