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