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