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