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