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