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