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