Java Reference
In-Depth Information
This program produces the output:
2+1=3
6.4. Shadowing and Obscuring
A local variable (§ 14.4 ) , formal parameter (§ 8.4.1 ), exception parameter (§ 14.20 ), and loc-
al class (§ 14.3 ) can only be referred to using a simple name (§ 6.2 ), not a qualified name
6.6 ) .
Some declarations are not permitted within the scope of a local variable, formal parameter,
exception parameter, or local class declaration because it would be impossible to distin-
guish between the declared entities using only simple names.
For example, if the name of a formal parameter of a method could be redeclared as the
name of a local variable in the method body, then the local variable would shadow the
formal parameter and the formal parameter would no longer be visible - an undesir-
able outcome.
It is a compile-time error if the name of a formal parameter is redeclared as a local variable
of the method or constructor; or as an exception parameter of a catch clause in a try statement
in the body of the method or constructor; or as a resource in a try -with-resources statement
in the body of the method or constructor.
It is a compile-time error if the name of a local variable v is redeclared as a local variable of
the directly enclosing method, constructor, or initializer block within the scope of v ; or as
an exception parameter of a catch clause in a try statement of the directly enclosing method,
constructor or initializer block within the scope of v ; or as a resource in a try -with-resources
statement of the directly enclosing method, constructor or initializer block within the scope
of v .
It is a compile-time error if the name of a local class C is redeclared as a local class of the
directly enclosing method, constructor, or initializer block within the scope of C .
It is a compile-time error if the name of an exception parameter is redeclared within the
Block of the catch clause as a local variable of the directly enclosing method, constructor,
or initializer block; or as an exception parameter of a catch clause in a try statement of the
directly enclosing method, constructor or initializer block; or as a resource in a try -with-re-
sources statement of the directly enclosing method, constructor or initializer block.
It is a compile-time error if the name of a variable declared in a ResourceSpecification of
a try -with-resources statement (§ 14.20.3 ) is redeclared within the try Block as a local vari-
able of the directly enclosing method, constructor, or initializer block, or as an exception
Search WWH ::




Custom Search