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