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