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