Determining Enclosing Instances

15.9.2 Determining Enclosing Instances

Let C be the class being instantiated, and let i be the instance being created. If C is an inner class then i may have an immediately enclosing instance. The immediately enclosing instance of i (§8.1.3) is determined as follows.

If C is an anonymous class, then: • If the class instance creation expression occurs in a static context (§8.1.3), then

i has no immediately enclosing instance. • Otherwise, the immediately enclosing instance of i is this .

15.9.2 Determining Enclosing Instances EXPRESSIONS

If C is a local class (§14.3), then let O be the innermost lexically enclosing class of

C . Let n be an integer such that O is the n'th lexically enclosing class of the class in which the class instance creation expression appears. Then:

• If C occurs in a static context, then i has no immediately enclosing instance. • Otherwise, if the class instance creation expression occurs in a static context,

then a compile-time error occurs. • Otherwise, the immediately enclosing instance of i is the n'th lexically enclosing

instance of this (§8.1.3). Otherwise, C is an inner member class (§8.5), and then: • If the class instance creation expression is an unqualified class instance creation

expression, then: ◆ If the class instance creation expression occurs in a static context, then a

compile-time error occurs. ◆ Otherwise, if C is a member of an enclosing class then let O be the innermost

lexically enclosing class of which C is a member, and let n be an integer such that O is the n'th lexically enclosing class of the class in which the class instance creation expression appears.

The immediately enclosing instance of i is the n'th lexically enclosing instance of this .

◆ Otherwise, a compile-time error occurs. • Otherwise, the class instance creation expression is a qualified class instance

creation expression. The immediately enclosing instance of i is the object that is the value of the

Primary expression. In addition, if C is an anonymous class, and the direct superclass of C , S , is an inner

class, then i may have an immediately enclosing instance with respect to S . It is determined as follows.

If S is a local class (§14.3), then let O be the innermost lexically enclosing class of S . Let n be an integer such that O is the n'th lexically enclosing class of the class in

which the class instance creation expression appears. Then: • If S occurs within a static context, then i has no immediately enclosing instance

with respect to S . • Otherwise, if the class instance creation expression occurs in a static context,

then a compile-time error occurs.

EXPRESSIONS Choosing the Constructor and its Arguments 15.9.3

• Otherwise, the immediately enclosing instance of i with respect to S is the n'th lexically enclosing instance of this .

Otherwise, S is an inner member class (§8.5), and then: • If the class instance creation expression is an unqualified class instance creation

expression, then: ◆ If the class instance creation expression occurs in a static context, then a

compile-time error occurs. ◆ Otherwise, if S is a member of an enclosing class, then let O

be the innermost lexically enclosing class of which S is a member, and let n be an integer such that O is the n'th lexically enclosing class of the class in which the class instance creation expression appears.

The immediately enclosing instance of i with respect to S is the n'th lexically enclosing instance of this .

◆ Otherwise, a compile-time error occurs. • Otherwise, the class instance creation expression is a qualified class instance

creation expression. The immediately enclosing instance of i with respect to S is the object that is the

value of the Primary expression.