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