JVM Knowledge

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 i has no immediately enclosing instance.

      If the direct superclass of C is an inner local class L that occurs in a static context, then let S be the nearest static method declaration, static field declaration, or static initializer that encloses the declaration of L. If the nearest static method declaration, static field 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 i is this.

  • If C is an inner local class, then:

    • If C occurs in a static context, then i has no immediately enclosing instance.

      Let S be the nearest static method declaration, static field declaration, or static initializer that encloses the declaration of C. If the nearest static method declaration, static field 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 i is the n'th lexically enclosing instance of this.

  • 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 i is the n'th lexically enclosing instance of this.

    • If the class instance creation expression is qualified, then the immediately enclosing instance of i is 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 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.

    • 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 i with respect to S is the n'th lexically enclosing instance of this.

  • 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 i with respect to S is the n'th lexically enclosing instance of this.

      • Otherwise, a compile-time error occurs.

    • If the class instance creation expression is qualified, then the immediately enclosing instance of i with respect to S is the object that is the value of the Primary expression or the ExpressionName.