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