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