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