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