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