Java Reference
In-Depth Information
Example 6.2-1. Identifiers and Obscuring
The following code was taken from a version of the class
String
and its method
indexOf
,
where the label was originally called
test
. Changing the label to have the same name as
of
i
. Thus, the code is valid.
class Test {
char[] value;
int offset, count;
int indexOf(TestString str, int fromIndex) {
char[] v1 = value, v2 = str.value;
int max = offset + (count - str.count);
int start = offset + ((fromIndex < 0) ? 0 : fromIndex);
i:
for (int i = start; i <= max; i++) {
int n = str.count, j = i, k = str.offset;
while (n-- != 0) {
if (v1[j++] != v2[k++])
continue i;
}
return i - offset;
}
return -1;
}
}
The identifier
max
could also have been used as the statement label; the label would
not obscure the local variable
max
within the labeled statement.
6.3. Scope of a Declaration
The
scope
of a declaration is the region of the program within which the entity declared by
A declaration is said to be
in scope
at a particular point in a program if and only if the de-
claration's scope includes that point.
The scope of the declaration of an observable (§
7.4.3
) top level package is all observable
compilation units (§
7.3
).
The declaration of a package that is not observable is never in scope. The declaration of a
subpackage is never in scope.