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