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