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