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