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