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