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