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