Spiga

How the (Java) Compiler Recognize the Meanings of Names

When you use a particular name in your program, the compiler has to determine what that name refers to, so that it can decide if you are using the name correctly and so that it can generate the appropriate code. If names are interpreted based on the context in which they are used, the programmer gets the convenience of reusing names (such as you and I often using the name i for a for loop counter), but the compiler has to be able to determine what each name means and so does any human being reading the code.

Name management is achieved with two mechanisms.

#1 The name space is partitioned to give different name spaces for different kinds of names.

Different name spaces allow you to give the same name to a method and a field (not recommended).
There are six different name spaces:

• package names,
• type names,
• field names,
• method names,
• local variable names (including parameters), and
• labels

#2 Scoping is used to control the visibility of names declared in one part of a program to other parts.


Scoping allows you to use the same name for all your for loop counters. Every declaration of a name has a scope in which that name can be used. The exact rules differ depending on the kind of nametype name, member name, local variable, and so on. A name cannot be used outside its scope for. However, scopes also nest and an inner scope has access to all names declared in the outer scope before the inner scope is entered.


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 declarations of that name in the different name spaces. The search order is:

1. Local variables declared in the code block,

2. for loop, or as parameters 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).
3. If the code is in a method or constructor, the parameters to the method or constructor.
4. A field of the class or interface, including any accessible inherited fields.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 static import statement.
6. A static field of a class, or interface, declared in a static import on demand statement.

If a name appears in a place where a type name is expected, then the different type scopes must be searched for that name. Type scopes are defined by packages. The search order is as follows:


1. The current type including inherited types.

2. A nested type of the current type.
3. Explicitly named imported types.
4. Other types declared in the same package.
5. Implicitly named imported types.

By knowing how the compiler works with Names in your program, you have a choice to make Names easy for both of you (as programmer) and the compiler to work with. And don’t forget make a documentations for your programs :)