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