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