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