Java Reference
In-Depth Information
outer scope before the inner scope is entered. For example, the body of
a for loop can access the local variables of the method in which it was
declared.
When a name that could be a variable is used, the meaning of the name
is determined by searching the current and enclosing scopes for declar-
ations of that name in the different namespaces. The search order is:
1. Local variables declared in the code block, for loop, or as para-
meters to the catch clause of a try statement. Then local variables
declared in any enclosing code block. This applies recursively up
to the method containing the block, or until there is no enclosing
block (as in the case of an initialization block).
2. If the code is in a method or constructor, the parameters to the
method or constructor.
3. A field of the class or interface, including any accessible inherited
fields.
4. If the type is a nested type, a variable in the enclosing block or
field of the enclosing class. If the type is a static nested type, only
static fields of an enclosing type are searched. This search rule
is applied successively to any enclosing blocks and types further
out.
5. A static field of a class, or interface, specifically declared in a stat-
ic import statement.
6. A static field of a class, or interface, declared in a static import on
demand statement.
For method names a similar process as for fields is followed, but starting
at step 3, searching for methods in the current class or interface. There
are special rules for determining how members of a class are accessed,
as you'll see in " Member Access " on page 223 .
The order of searching determines which declaration will be found. This
implies that names declared in outer scopes can be hidden by names de-
clared in inner scopes. And that means, for example, that local variable
 
Search WWH ::




Custom Search