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.
    • Otherwise, the immediately enclosing instance of i is this.
  • If C is a local class, 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, let O be the immediately enclosing class of C. Let n be an integer such that O is the n'th lexically enclosing type declaration 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.

  • 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 a member of a class enclosing the class in which the class instance creation expression appears, then let O be the immediately enclosing class of which C is a member. Let n be an integer such that O is the n'th lexically enclosing type declaration 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.

    • 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 a 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 type declaration of S. Let n be an integer such that O is the n'th lexically enclosing type declaration 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.

  • 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 a member of a class enclosing the class in which the class instance creation expression appears, then let O be the immediately enclosing class of which S is a member. Let n be an integer such that O is the n'th lexically enclosing type declaration 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.

    • 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.