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