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