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