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