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