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