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