Understanding Class Loader Attributes

Now that you’ve seen the standard Java class loaders and the delegation model that governs how these class loaders interact, let’s talk more about how class loaders work.

Loading Classes on Demand

The three class loaders don’t preload all the classes in their scope. Instead, they load the classes on demand. This is called lazy loading because the class loader doesn’t load the data until it’s requested. Although laziness in human beings is generally regarded as negative, it’s actually quite a good thing for class loaders. The reasons are as follows:

  • Faster performance: If each class loader had to load every class, it would take much longer to initialize the JVM.

  • Efficiency: Loading in the classes would consume more memory than necessary if loaded early.

  • Flexibility: JAR files and classes can be added to the search paths of all the class loaders even after the class loaders have been initialized.

Note that when a class is loaded, all its parent classes must also be loaded. Thus, if ClassB extends ClassA, and ClassB is loaded, then ClassA is also loaded.

Class Caching

The standard J2SE class loaders look up classes on demand, but once a class is loaded into a class loader, it will stay loaded (cached) for as long as the JVM is running.

Separate Namespaces

Each class loader has its own unique namespace. In other words, if the bootstrap class loader loads a class named sun.misc.ClassA, and the system class loader loads a class named sun.misc.ClassB, the two classes will be considered to be in distinct packages and each class won’t have access to the other class’s package-private members.

Creating a Custom Class Loader

A developer can even create custom class loaders, though it may seem like a pointless exercise. However, creating custom class loaders is fairly easy and doing so can in fact give an application an incredible amount of flexibility. While this is beyond the scope of this book, it’s worth noting that Tomcat extensively uses custom class loaders.



Pro Jakarta Tomcat 5
Pro Apache Tomcat 5/5.5 (Experts Voice in Java)
ISBN: 1590593316
EAN: 2147483647
Year: 2004
Pages: 94

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net