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
the local variable i does not obscure (§ 6.4.2 ) the label in the scope of the declaration
of i . Thus, the code is valid.
Click here to view code image
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
the declaration can be referred to using a simple name, provided it is visible (§ 6.4.1 ) .
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.
Search WWH ::




Custom Search