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