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