JVM Knowledge

The following steps are used to derive a nonarray class or interface C denoted by N from a purported representation in class file format using the class loader L.

  1. First, the Java Virtual Machine determines whether L has already been recorded as an initiating loader of a class or interface denoted by N. If so, this derivation attempt is invalid and derivation throws a LinkageError.

  2. Otherwise, the Java Virtual Machine attempts to parse the purported representation. The purported representation may not in fact be a valid representation of C, so derivation must detect the following problems:

    • If the purported representation is not a ClassFile structure (§4.1, §4.8), derivation throws a ClassFormatError.

    • Otherwise, if the purported representation is not of a supported major or minor version (§4.1), derivation throws an UnsupportedClassVersionError.

      UnsupportedClassVersionError, a subclass of ClassFormatError, was introduced in JDK 1.2 to enable easy identification of a ClassFormatError caused by an attempt to load a class whose representation uses an unsupported version of the class file format. In JDK 1.1 and earlier, an instance of NoClassDefFoundError or ClassFormatError was thrown in case of an unsupported version, depending on whether the class was being loaded by the system class loader or a user-defined class loader.

    • Otherwise, if the purported representation does not actually represent a class or interface named N, derivation throws a NoClassDefFoundError.

      This occurs when the purported representation has either a this_class item which specifies a name other than N, or an access_flags item which has the ACC_MODULE flag set.

  3. If C has a direct superclass, the symbolic reference from C to its direct superclass is resolved using the algorithm of §5.4.3.1. Note that if C is an interface it must have Object as its direct superclass, which must already have been loaded. Only Object has no direct superclass.

    Any exception that can be thrown as a result of failure of class or interface resolution can be thrown as a result of derivation. In addition, derivation must detect the following problems:

    • If any of the superclasses of C is C itself, derivation throws a ClassCircularityError.

    • Otherwise, if the class or interface named as the direct superclass of C is in fact an interface or a final class, derivation throws an IncompatibleClassChangeError.

    • Otherwise, if the class named as the direct superclass of C has a PermittedSubclasses attribute (§4.7.31) and any of the following is true, derivation throws an IncompatibleClassChangeError:

      • The superclass is in a different run-time module than C (§5.3.6).
      • C does not have its ACC_PUBLIC flag set (§4.1) and the superclass is in a different run-time package than C (§5.3).
      • No entry in the classes array of the superclass's PermittedSubclasses attribute refers to a class or interface with the name N.
    • Otherwise, if C is a class and some instance method declared in C can override (§5.4.5) a final instance method declared in a superclass of C, derivation throws an IncompatibleClassChangeError.

  4. If C has any direct superinterfaces, the symbolic references from C to its direct superinterfaces are resolved using the algorithm of §5.4.3.1.

    Any exception that can be thrown as a result of failure of class or interface resolution can be thrown as a result of derivation. In addition, derivation must detect the following problems:

    • If any of the superinterfaces of C is C itself, derivation throws a ClassCircularityError.

    • Otherwise, if any class or interface named as a direct superinterface of C is not in fact an interface, derivation throws an IncompatibleClassChangeError.

    • Otherwise, for each direct superinterface named by C, if the superinterface has a PermittedSubclasses attribute (§4.7.31) and any of the following is true, derivation throws an IncompatibleClassChangeError:

      • The superinterface is in a different run-time module than C.
      • C does not have its ACC_PUBLIC flag set (§4.1) and the superinterface is in a different run-time package than C.
      • No entry in the classes array of the superinterface's PermittedSubclasses attribute refers to a class or interface with the name N.

If no exception is thrown in steps 1-4, then derivation of the class or interface C succeeds. The Java Virtual Machine marks C to have L as its defining loader, records that L is an initiating loader of C (§5.3.4), and creates C in the method area (§2.5.4).

When derivation succeeds, the process of loading and creating C is not complete until every class loader that was involved in loading C (directly or indirectly) returns C as its result. Depending on the behavior of user-defined class loaders, the process of loading and creating C may yet fail (§5.3.2).

If an exception is thrown in steps 1-4, then derivation of the class or interface C fails with that exception.