Given the following code:
public class Dred {
public static void main(String[] args) {
Dred d0 = new Dred();
A
Dred d1 = new Dred();
B
d0.hodpel(1);
d1.hodpel(10);
d1 = new Dred();
d0 = new Dred();
d0.hodpel(100);
d1.hodpel(1000);
}
private int pibo = 0;
private static int imde = 0;
public void hodpel(int he) {
C
int os = 0;
pibo += he;
imde += he;
os += he;
System.out.println("pibo=" + pibo + " imde=" + imde + " os=" + os);
}
}
os, pibo, imde, d0, d1] are in scope at A ?Output:
os=1 pibo=1 imde=1 os=10 pibo=11 imde=10 os=100 pibo=111 imde=100 os=1000 pibo=1111 imde=1000
In scope at A : pibo, d0, d1
In scope at B : pibo, d0, d1
In scope at C : pibo, os, imde
Explanation (which you do not need to write out in your submitted solution):
pibo is a static variable, os is an instance variable, and imde is a local variable.
At A , os is out of scope because it is an instance variable, but main is a static method. imde is out of scope because it is local to hodpel.
At B , os is out of scope because it is an instance variable, but main is a static method. imde is out of scope because it is local to hodpel.
At C , d0 and d1 out of scope because they are local to the main method.
Related puzzles: