Java Reference
In-Depth Information
class AnotherLocal {
void bar() {
class Local {} // ok
}
}
}
class Local {} // ok, not in scope of prior Local
}
}
The first statement of method
foo
creates an instance of the member class
Global.Cyclic
rather than an instance of the local class
Cyclic
, because the local class declaration is
not yet in scope.
The fact that the scope of a local class encompasses its own declaration (not only its
body) means that the definition of the local class
Cyclic
is indeed cyclic because it ex-
tends itself rather than
Global.Cyclic
. Consequently, the declaration of the local class
Cyclic
will be rejected at compile time.
Since local class names cannot be redeclared within the same method (or constructor
or initializer, as the case may be), the second and third declarations of
Local
result in
compile-time errors. However,
Local
can be redeclared in the context of another, more
deeply nested, class such as
AnotherLocal
.
The fourth and last declaration of
Local
is legal, since it occurs outside the scope of
any prior declaration of
Local
.
14.4. Local Variable Declaration Statements
A
local variable declaration statement
declares one or more local variable names.
LocalVariableDeclarationStatement:
LocalVariableDeclaration
;
LocalVariableDeclaration:
VariableModifiers
opt
Type VariableDeclarators
VariableModifiers:
VariableModifier
VariableModifiers VariableModifier
VariableModifier: one of
Annotation
final