The loading process is implemented by the class ClassLoader
(§20.14) and its
subclasses. Different subclasses of ClassLoader
may implement different loading policies. In particular, a class loader may cache binary representations of
classes and interfaces, prefetch them based on expected usage, or load a group of
related classes together. These activities may not be completely transparent to a
running Java application if, for example, a newly compiled version of a class is
not found because an older version is cached by a class loader. It is the responsibility of a class loader, however, to reflect loading errors only at points in the program they could have arisen without prefetching or group loading.
If an error occurs during class loading, then an instance of one of the following subclasses of class LinkageError
will be thrown at any point in the Java program that (directly or indirectly) uses the type:
ClassCircularityError
: A class or interface could not be loaded because it would be its own superclass or superinterface (§13.4.4).
ClassFormatError
: The binary data that purports to specify a requested compiled class or interface is malformed.
NoClassDefFoundError
: No definition for a requested class or interface could be found by the relevant class loader.
Because loading involves the allocation of new data structures, it may fail with an OutOfMemoryError
.