Java Reference
In-Depth Information
spect to S . The immediately enclosing instance of an object with respect to its class' direct
superclass, if any, is determined when the superclass constructor is invoked via an explicit
constructor invocation statement.
When an inner class (whose declaration does not occur in a static context) refers to an in-
stance variable that is a member of a lexically enclosing class, the variable of the corres-
ponding lexically enclosing instance is used.
Any local variable, formal parameter, or exception parameter used but not declared in an
inner class must be declared final .
Any local variable used but not declared in an inner class must be definitely assigned (§16)
before the body of the inner class.
A blank final 4.12.4 ) field of a lexically enclosing class may not be assigned within an
inner class, or a compile-time error occurs.
Example 8.1.3-2. Inner Class Declarations
Click here to view code image
class Outer {
int i = 100;
static void classMethod() {
final int l = 200;
class LocalInStaticContext {
int k = i; // Compile-time error
int m = l; // OK
}
}
void foo() {
class Local { // A local class
int j = i;
}
}
}
The declaration of class LocalInStaticContext occurs in a static context due to being with-
in the static method classMethod . Instance variables of class Outer are not available
within the body of a static method. In particular, instance variables of Outer are not
available inside the body of LocalInStaticContext . However, local variables from the sur-
rounding method may be referred to without error (provided they are marked final ).
Inner classes whose declarations do not occur in a static context may freely refer to
the instance variables of their enclosing class. An instance variable is always defined
Search WWH ::




Custom Search