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 (§8.1.3), determined as follows:
-
If C is an anonymous class, then:
-
If the class instance creation expression occurs in a static context, then
ihas no immediately enclosing instance.If the direct superclass of C is an inner local class
Lthat occurs in a static context, then let S be the neareststaticmethod declaration,staticfield declaration, or static initializer that encloses the declaration ofL. If the neareststaticmethod declaration,staticfield declaration, or static initializer that encloses the class instance creation expression is not S, then a compile-time error occurs. -
Otherwise, the immediately enclosing instance of
iisthis.
-
-
If C is an inner local class, then:
-
If C occurs in a static context, then
ihas no immediately enclosing instance.Let S be the nearest
staticmethod declaration,staticfield declaration, or static initializer that encloses the declaration of C. If the neareststaticmethod declaration,staticfield declaration, or static initializer that encloses the class instance creation expression is not S, then a compile-time error occurs. -
Otherwise, if the class instance creation expression occurs in a static context, then a compile-time error occurs.
-
Otherwise, let O be the immediately enclosing class or interface declaration of C, and let U be the immediately enclosing class or interface declaration of the class instance creation expression.
If U is not an inner class of O or O itself, then a compile-time error occurs.
Let n be an integer such that O is the n'th lexically enclosing class or interface declaration of U.
The immediately enclosing instance of
iis the n'th lexically enclosing instance ofthis.
-
-
If C is an inner member class, then:
-
If the class instance creation expression is unqualified, then:
-
If the class instance creation expression occurs in a static context, then a compile-time error occurs.
-
Otherwise, if C is not a member of any class whose declaration lexically encloses the class instance creation expression, then a compile-time error occurs.
-
Otherwise, let O be the innermost enclosing class declaration of which C is a member, and let U be the immediately enclosing class or interface declaration of the class instance creation expression.
If U is not an inner class of O or O itself, then a compile-time error occurs.
If the class instance creation expression occurs in the early construction context (§8.8.7) of O, then a compile-time error occurs.
Let n be an integer such that O is the n'th lexically enclosing class or interface declaration of U.
The immediately enclosing instance of
iis the n'th lexically enclosing instance ofthis.
-
-
If the class instance creation expression is qualified, then the immediately enclosing instance of
iis the object that is the value of the Primary expression or the ExpressionName.
-
If C is an anonymous class, and its direct superclass S is an inner class, then i may have an immediately enclosing instance with respect to S, determined as follows:
-
If S is an inner local class, then:
-
If S occurs in a static context, then
ihas 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.
-
Otherwise, let O be the immediately enclosing class or interface declaration of S, and let U be the immediately enclosing class or interface declaration of the class instance creation expression.
If U is not an inner class of O or O itself, then a compile-time error occurs.
Let n be an integer such that O is the n'th lexically enclosing class or interface declaration of U.
The immediately enclosing instance of
iwith respect to S is the n'th lexically enclosing instance ofthis.
-
-
If S is an inner member class, then:
-
If the class instance creation expression is unqualified, then:
-
If the class instance creation expression occurs in a static context, then a compile-time error occurs.
-
Otherwise, if S is not a member of any class whose declaration encloses the class instance creation expression, then a compile-time error occurs.
-
Otherwise, let O be the innermost enclosing class declaration of which S is a member, and let U be the immediately enclosing class or interface declaration of the class instance creation expression.
If U is not an inner class of O or O itself, then a compile-time error occurs.
If the class instance creation expression occurs in the early construction context of O, then a compile-time error occurs.
Let n be an integer such that O is the n'th lexically enclosing class or interface declaration of U.
The immediately enclosing instance of
iwith respect to S is the n'th lexically enclosing instance ofthis. -
Otherwise, a compile-time error occurs.
-
-
If the class instance creation expression is qualified, then the immediately enclosing instance of
iwith respect to S is the object that is the value of the Primary expression or the ExpressionName.
-