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