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