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