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