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