Java Reference
In-Depth Information
This program produces the output:
2+1=3
6.4. Shadowing and Obscuring
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
able of the directly enclosing method, constructor, or initializer block, or as an exception