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