Given the following code:
public class HulMehu {
private static int tud = 0;
private int up = 0;
public void scang(int ie) {
int eaer = 0;
tud += ie;
up += ie;
eaer += ie;
System.out.println("tud=" + tud + " up=" + up + " eaer=" + eaer);
A
}
public static void main(String[] args) {
HulMehu h0 = new HulMehu();
B
HulMehu h1 = new HulMehu();
C
h0.scang(1);
h1 = h0;
h0 = new HulMehu();
h1.scang(10);
h0.scang(100);
h1.scang(1000);
}
}
eaer, tud, up, h0, h1] are in scope at A ?Output:
eaer=1 tud=1 up=1 eaer=11 tud=11 up=10 eaer=111 tud=100 up=100 eaer=1111 tud=1011 up=1000
In scope at A : eaer, tud
In scope at B : eaer, h0, h1
In scope at C : eaer, h0, h1
Explanation (which you do not need to write out in your submitted solution):
eaer is a static variable, tud is an instance variable, and up is a local variable.
At A , up 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 , tud is out of scope because it is an instance variable, but main is a static method. up is out of scope because it is local to scang.
At C , tud is out of scope because it is an instance variable, but main is a static method. up is out of scope because it is local to scang.
Related puzzles: