Given the following code:
public class StiRisne {
private int cawe = 0;
public static void main(String[] args) {
StiRisne s0 = new StiRisne();
A
StiRisne s1 = new StiRisne();
B
s0.sedspi(1);
s1 = s0;
s0 = new StiRisne();
s1.sedspi(10);
s0.sedspi(100);
s1.sedspi(1000);
}
public void sedspi(int iho) {
int onta = 0;
ra += iho;
onta += iho;
cawe += iho;
System.out.println("ra=" + ra + " onta=" + onta + " cawe=" + cawe);
C
}
private static int ra = 0;
}
cawe, ra, onta, s0, s1] are in scope at A ?Output:
cawe=1 ra=1 onta=1 cawe=11 ra=10 onta=11 cawe=111 ra=100 onta=100 cawe=1111 ra=1000 onta=1011
In scope at A : cawe, s0, s1
In scope at B : cawe, s0, s1
In scope at C : cawe, onta
Explanation (which you do not need to write out in your submitted solution):
cawe is a static variable, onta is an instance variable, and ra is a local variable.
At A , onta is out of scope because it is an instance variable, but main is a static method. ra is out of scope because it is local to sedspi.
At B , onta is out of scope because it is an instance variable, but main is a static method. ra is out of scope because it is local to sedspi.
At C , ra 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: