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