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