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