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