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