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