How Classes Are Loaded

You probably already know that Java source code compiles into .class files. Each .class file contains the bytecode the compiled instructions for the JVM for a single class. You probably also know that every Java class gets compiled separately, and the resulting bytecode goes into distinct.class files, which are then dynamically linked at runtime. Dynamic Linking Most traditional programming languages use a static linking model. In order to generate an executable application, the source code must be compiled, and all references must be immediately resolved. In other words, the individual components of the application must be linked together. Dynamic linking postpones the resolution process as long as possible. It is quite possible, with a running Java application, that some references are never actually resolved because, for example, the referenced method was never called. This may seem a little strange at first; C++ programmers often have a hard time wrapping their minds around it. But if you think about it, some form of dynamic linking is absolutely necessary in order for Java to work. Consider the typical applet scenario: • I write an applet on a Linux machine, using Suns version of JDK 1.2. • You download the web page on a Macintosh, and the applet executes inside MRJ, using Apples version of JDK 1.1.6. This sort of scenario is impossible with C++; linking has to be postponed until execution. Unfortunately, dynamic linking can also cause problems when applications are only partially updated. If you change only a few .class files, you might inadvertently have two .class files that cannot be linked together e.g., an old .class file calls a method that youve removed from one of the newer .class files. Chapt er 13 of The Java Laguage Specification, Second Edition edited by Bill Loy Addison-Wesley spells out in great detail the ways in which classes can change without breaking dynamic linking. Its well worth reading if you plan to use dynamic classloading as an integral part of your application. For ordinary application development, thats really all you need to know about how Java handles classes. However, we need to cover a little more in order to fully understand dynamic classloading.

19.2.1 How Classes Are Loaded

The Java virtual machine loads and validates individual classes through the use of objects known as classloaders. The classloading system works as follows: 1. When the JVM is first launched, a single classloader, often referred to as the bootstrap classloader, is created. This classloader is responsible for loading and creating most classes used within the JVM. 2. An application can programmatically create more classloaders. Every classloader, with the exception of the bootstrap classloader, has a parent classloader. The set of classloaders forms a tree with the bootstrap classloader at the root. 3. When a class object is needed, the JVM finds the appropriate classloader and asks it for the class object using one of the loadClass methods. The classloader first asks its parent for the class and, if the parent returns null, it then creates the class itself. The idea behind this is that classloaders are a way to partition the set of classes and isolate class definitions. Implicit in the last point, for example, is the fact that if two sibling classloaders have the same parent are asked for the same class, the class object will be loaded and initialized twice. Classloaders are useful in two distinct scenarios. The first is when you want to load class objects from a nonstandard source. The bootstrap classloader attempts to load classes from the file system using the CLASSPATH system variable. It expects that the package and name of a class will map cleanly to a .class file containing the bytecode for the class. If you want to load classes from a different source, for example from a URL, you need to install another classloader, such as java.net.URLClassLoader . The second scenario involves having different versions of the same class running at the same time. Because classloaders only check their parents, not their siblings, to see if a class has already been loaded, its possible to safely load different versions of the same class into a JVM. See Figur e 19- 1 . Figure 19-1. Classloaders define the scope of a class

19.3 How Dynamic Classloading Works