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