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