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