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