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