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