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