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