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