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