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