Lexical Scope of Declarations

The lexical scope of a declaration determines where the named entity is a valid identifier. Every declaration is associated with a lexical level that corresponds to one of the following Java constructs:

These lexical levels correspond to nested constructs. When the Java compiler encounters a name in a program, it finds the declaration for that name by first looking in the lexical level where the name is encountered. If the compiler does not find the name in that lexical level, it searches progressively higher lexical levels until it finds the declaration. If all of the lexical levels are exhausted, the compiler issues an error message.

If, however, an identifier is qualified by a class or package name, the compiler only searches that lexical level for a declaration.

References Blocks; Class Declarations; Interface Declarations; Packages; Methods; The for Statement