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