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