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