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