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