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