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