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