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