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