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