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