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