JVM Knowledge

Creation of a class or interface C denoted by the name N consists of the construction of an implementation-specific internal representation of C in the method area of the Java Virtual Machine (§2.5.4).

Class or interface creation is triggered by another class or interface D, whose run-time constant pool symbolically references C by means of the name N (§5.4.3.1). If N does not denote an array class, then the Java Virtual Machine relies on a class loader to locate a binary representation for a class or interface called N (§4.1). Once a class loader has located a binary representation, it relies in turn on the Java Virtual Machine to derive the class or interface C from the binary representation, and then to create C in the method area. Array classes do not have an external binary representation; they are created by the Java Virtual Machine via a different process.

Class or interface creation may also be triggered by D invoking methods in certain Java SE Platform class libraries (§2.12) such as reflection.

There are two kinds of class loaders: the bootstrap class loader supplied by the Java Virtual Machine, and user-defined class loaders. Every user-defined class loader is an instance of a subclass of the abstract class ClassLoader. Applications employ user-defined class loaders in order to extend the manner in which the Java Virtual Machine dynamically creates classes. User-defined class loaders can be used to create classes that originate from user-defined sources. For example, a class could be downloaded across a network, generated on the fly, or extracted from an encrypted file.

When the Java Virtual Machine asks a class loader L to locate a binary representation for a class or interface called N, L loads the class or interface C denoted by N. L may load C directly, by locating a binary representation and asking the Java Virtual Machine to derive and create C from the binary representation. Alternatively, L may load C indirectly, by delegating to another class loader which loads C directly or indirectly.

If L loads C directly, we say that L defines C or, equivalently, that L is the defining loader of C.

Whether L loads C directly or indirectly, we say that L initiates loading of C, or, equivalently, that L is an initiating loader of C.

Due to class loader delegation, the loader L_1 that initiates loading at the Java Virtual Machine's request may not be the same as the loader L_2 that completes loading by defining the class or interface. In this case, we say that each of L_1 and L_2 initiates loading of C, or, equivalently, that each of L_1 and L_2 is an initiating loader of C. Any loaders in a delegation chain between L_1 and L_2 are not considered to be initiating loaders of C.

We will sometimes represent a class or interface using the following notation, instead of using an identifier like C or D:

  • <N, L_d> - where N denotes the name of the class or interface and L_d denotes the defining loader of the class or interface.
  • N^{L_i} - where N denotes the name of the class or interface and L_i denotes an initiating loader of the class or interface.

It should be clear that loading a class or interface is a joint effort between the Java Virtual Machine and a class loader (or multiple class loaders, if delegation occurs). The ultimate outcome of loading is that the Java Virtual Machine creates a class or interface in its method area, so it is often convenient to say that a class or interface is loaded and thereby created.

The complex back-and-forth nature of loading, combined with the ability of user-defined class loaders to exhibit arbitrary behavior, means that exceptions can be thrown after the Java Virtual Machine has created a class or interface but before every class loader participating in loading has completed. This specification accounts for such exceptions in what is often referred to the process of loading and creating a class or interface.

The Java Virtual Machine uses one of three procedures to create a class or interface C denoted by the name N in the run-time constant pool of a class or interface D:

  • If N denotes either a nonarray class or an interface, and D was defined by the bootstrap class loader, then the bootstrap class loader initiates loading of C (§5.3.1).

  • If N denotes either a nonarray class or an interface, and D was defined by a user-defined class loader, then that same user-defined class loader initiates loading of C (§5.3.2).

  • If N denotes an array class, then the Java Virtual Machine creates an array class C denoted by N, in association with the defining loader of D (§5.3.3).

    Although the defining loader of D is relevant in the course of creating an array class, it is not used to load and thereby create the array class.

If an error occurs during loading of a class or interface - either when a class loader is locating a binary representation, or when the Java Virtual Machine is deriving and creating a class from it - then the error must be thrown at a point in the program that (directly or indirectly) uses the class or interface being loaded.

A well-behaved class loader should maintain three properties:

  • Given the same name, a good class loader should always return the same Class object.
  • If a class loader L_1 delegates loading of a class C to another loader L_2, then for any type T that occurs as the direct superclass or a direct superinterface of C, or as the type of a field in C, or as the type of a formal parameter of a method or constructor in C, or as a return type of a method in C, L_1 and L_2 should return the same Class object.
  • If a user-defined classloader prefetches binary representations of classes and interfaces, or loads a group of related classes together, then it must reflect loading errors only at points in the program where they could have arisen without prefetching or group loading.

After creation, a class or interface is determined not by its name alone, but by a pair: its binary name (§4.2.1) and its defining loader. Each such class or interface belongs to a single run-time package. The run-time package of a class or interface is determined by the package name and the defining loader of the class or interface.

Sections: