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