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