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