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