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