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