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