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