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