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