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