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