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