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