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