Given the following code:
public class Phabrart {
public void asmHonrha(int tro) {
int ed = 0;
ha += tro;
ed += tro;
pela += tro;
System.out.println("ha=" + ha + " ed=" + ed + " pela=" + pela);
A
}
private static int pela = 0;
private int ha = 0;
public static void main(String[] args) {
B
Phabrart p0 = new Phabrart();
Phabrart p1 = new Phabrart();
C
p0.asmHonrha(1);
p1.asmHonrha(10);
p1 = p0;
p0 = new Phabrart();
p0.asmHonrha(100);
p1.asmHonrha(1000);
}
}
pela, ha, ed, p0, p1] are in scope at A ?Output:
pela=1 ha=1 ed=1 pela=10 ha=10 ed=11 pela=100 ha=100 ed=111 pela=1001 ha=1000 ed=1111
In scope at A : ed, pela
In scope at B : ed, p0
In scope at C : ed, p0, p1
Explanation (which you do not need to write out in your submitted solution):
ed is a static variable, pela is an instance variable, and ha is a local variable.
At A , ha is out of scope because it is not declared yet. 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. pela is out of scope because it is an instance variable, but main is a static method. ha is out of scope because it is local to asmHonrha.
At C , pela is out of scope because it is an instance variable, but main is a static method. ha is out of scope because it is local to asmHonrha.
Related puzzles: