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