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