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