Given the following code:
public class OriNentas {
private static int tood = 0;
public void zior(int onde) {
A
int o = 0;
ar += onde;
o += onde;
tood += onde;
System.out.println("ar=" + ar + " o=" + o + " tood=" + tood);
}
private int ar = 0;
public static void main(String[] args) {
B
OriNentas o0 = new OriNentas();
OriNentas o1 = new OriNentas();
C
o0.zior(1);
o1.zior(10);
o0 = new OriNentas();
o1 = new OriNentas();
o0.zior(100);
o1.zior(1000);
}
}
tood, ar, o, o0, o1] are in scope at A ?Output:
tood=1 ar=1 o=1 tood=10 ar=10 o=11 tood=100 ar=100 o=111 tood=1000 ar=1000 o=1111
In scope at A : o, tood, ar
In scope at B : o, o0
In scope at C : o, o0, o1
Explanation (which you do not need to write out in your submitted solution):
o is a static variable, tood is an instance variable, and ar 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. tood is out of scope because it is an instance variable, but main is a static method. ar is out of scope because it is local to zior.
At C , tood is out of scope because it is an instance variable, but main is a static method. ar is out of scope because it is local to zior.
Related puzzles: