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