10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8.1.3 Inner Classes and Enclosing Instances CLASSES<br />

182<br />

text. A class C is an inner class of class O if it is either a direct inner class of O or<br />

an inner class of an inner class of O.<br />

A class O is the zeroth lexically enclosing class of itself. A class O is the nth<br />

lexically enclosing class of a class C if it is the immediately enclosing class of the<br />

n – 1 st lexically enclosing class of C.<br />

An instance i of a direct inner class C of a class O is associated with an<br />

instance of O, known as the immediately enclosing instance of i. <strong>The</strong> immediately<br />

enclosing instance of an object, if any, is determined when the object is created<br />

(§15.9.2).<br />

An object o is the zeroth lexically enclosing instance of itself. An object o is<br />

the nth lexically enclosing instance of an instance i if it is the immediately<br />

enclosing instance of the n –<br />

1 st lexically enclosing instance of i.<br />

When an inner class refers to an instance variable that is a member of a lexically<br />

enclosing class, the variable of the corresponding lexically enclosing<br />

instance is used. A blank final (§4.12.4) field of a lexically enclosing class may<br />

not be assigned within an inner class.<br />

An instance of an inner class I whose declaration occurs in a static context<br />

has no lexically enclosing instances. However, if I is immediately declared within<br />

a static method or static initializer then I does have an enclosing block, which is<br />

the innermost block statement lexically enclosing the declaration of I.<br />

Furthermore, for every superclass S of C which is itself a direct inner class of a<br />

class SO, there is an instance of SO associated with i, known as the immediately<br />

enclosing instance of i with respect to S. <strong>The</strong> immediately enclosing instance of an<br />

object with respect to its class’ direct superclass, if any, is determined when the<br />

superclass constructor is invoked via an explicit constructor invocation statement.<br />

Any local variable, formal method parameter or exception handler parameter<br />

used but not declared in an inner class must be declared final. Any local variable,<br />

used but not declared in an inner class must be definitely assigned (§16)<br />

before the body of the inner class.<br />

Inner classes include local (§14.3), anonymous (§15.9.5) and non-static member<br />

classes (§8.5). Here are some examples:<br />

class Outer {<br />

int i = 100;<br />

static void classMethod() {<br />

final int l = 200;<br />

class LocalInStaticContext{<br />

int k = i; // compile-time error<br />

int m = l; // ok<br />

}<br />

}<br />

DRAFT

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!