Packages This Package Prev Next Index
public abstract class java.lang.ClassLoader extends java.lang.Object (I-§1.12) { // Constructors protected ClassLoader(); §1.4.1 // Methods protected final Class §1.4.2 defineClass(byte data[], int offset, int length); protected final Class findSystemClass(String name); §1.4.3 protected abstract Class §1.4.4 loadClass(String name, boolean resolve); protected final void resolveClass(Class c); §1.4.5 }The class ClassLoader is an abstract class. Applications implement subclasses of ClassLoader in order to extend the manner in which the Java Virtual Machine dynamically loads classes.
However, some classes may not originate from a file; they may originate from other sources, such as the network, or they could be constructed by an application. The method defineClass (I-§1.4.2) converts an array of bytes into an instance of class Class (I-§1.3). Instances of this newly defined class can be created using the newInstance method in class Class (I-§1.3.7).
The methods and constructors of objects created by a class loader may reference other classes. To determine the class(es) referred to, the Java Virtual Machine calls the loadClass method (I-§1.4.4) of the class loader that originally created the class. If the Java Virtual Machine only needs to determine if the class exists and if it does exist to know its superclass, the resolve flag is set to false. However if an instance of the class is being created or any of its methods are being called, the class must also be resolved. In this case the resolve flag is set to true, and the resolveClass method (I-§1.4.5) should be called.
ClassLoader loader = new NetworkClassLoader(host, port);
Object main = loader.loadClass("Main", true).newInstance();
....
class NetworkClassLoader { String host; int port; Hashtable cache = new Hashtable(); private byte loadClassData(String name)[] { // load the class data from the connection ... }
public synchronized Class loadClass(String name, boolean resolve) { Class c = cache.get(name); if (c == null) { byte data[] = loadClassData(name); c = defineClass(data, 0, data.length); cache.put(name, c); } if (resolve) resolveClass(c); return c;
} }
protected ClassLoader()
protected final Class
defineClass(byte data[], int offset, int length)
data
-
the bytes that make up the Class
offset
-
the start offset of the Class data
length
-
the length of the Class data
protected final Class findSystemClass(String name)
throws ClassNotFoundException
name
-
the name of the system Class
protected abstract Class
loadClass(String name, boolean resolve)
throws ClassNotFoundException
name
-
the name of the desired Class
resolve
-
true if the Class must be resolved
protected final void resolveClass(Class c)
c
-
the Class instance to be resolved
Packages This Package Prev Next IndexJava API Document (HTML generated by dkramer on April 22, 1996)