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