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