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