Class Loading Architecture

We’ll show some examples of how to create and use class loaders a bit later in this chapter. First, let’s examine from a logical perspective how class loaders work.

Class loaders are organized into a tree hierarchy. At the root of this tree is the system class loader. This class loader is also called the primordial class loader or the null class loader. It is used only to load classes from the core Java API.

The system class loader has one or more children. It has at least one child; the URL class loader that is used to load classes from the classpath. It may have other direct children, though typically any other class loaders are children of the URL class loader that reads the classpath. An example class loader hierarchy is shown in Figure 6-2; this is the hierarchy that might exist within the Java Plug-in after it has loaded applets from both www.sun.com and www.ora.com.

A class loader hierarchy

Figure 6-2. A class loader hierarchy

The hierarchy comes into play when it is time to load a class. Classes are loaded in one of three ways: either explicitly by calling the loadClass( ) method of a class loader, explicitly by calling the Class.forName( ) method, or implicitly when they are referenced by an already-loaded class.

In any case, a class loader is asked to load the class. In the first case, the class loader is the object on which the loadClass( ) method is invoked. In ...

Get Java Security, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.