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