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