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