Java Reference
In-Depth Information
6.4.2. Obscuring
A simple name may occur in contexts where it may potentially be interpreted as the name
of a variable, a type, or a package. In these situations, the rules of § 6.5 specify that a vari-
able will be chosen in preference to a type, and that a type will be chosen in preference to
a package. Thus, it is may sometimes be impossible to refer to a visible type or package
declaration via its simple name. We say that such a declaration is obscured .
Obscuring is distinct from shadowing (§ 6.4.1 ) and hiding (§ 8.3 , § 8.4.8.2 , § 8.5 , § 9.3 , § 9.5 ) .
The naming conventions of § 6.1 help reduce obscuring, but if it does occur, here are
some notes about what you can do to avoid it.
When package names occur in expressions:
• If a package name is obscured by a field declaration, then import declarations
7.5 ) can usually be used to make available the type names declared in that
package.
• If a package name is obscured by a declaration of a parameter or local vari-
able, then the name of the parameter or local variable can be changed
without affecting other code.
The first component of a package name is normally not easily mistaken for a type
name, as a type name normally begins with a single uppercase letter. (The Java pro-
gramming language does not actually rely on case distinctions to determine whether a
name is a package name or a type name.)
Obscuring involving class and interface type names is rare. Names of fields, paramet-
ers, and local variables normally do not obscure type names because they convention-
ally begin with a lowercase letter whereas type names conventionally begin with an
uppercase letter.
Method names cannot obscure or be obscured by other names (§ 6.5.7 ) .
Obscuring involving field names is rare; however:
• If a field name obscures a package name, then an import declaration (§ 7.5 )
can usually be used to make available the type names declared in that pack-
age.
• If a field name obscures a type name, then a fully qualified name for the type
can be used unless the type name denotes a local class (§ 14.3 ) .
• Field names cannot obscure method names.
Search WWH ::




Custom Search