Linking Verifying that the class's bytecodes conform to the language rules, preparing the virtual
machine by allocating space for static fields, and (optionally) resolving all references in the class by,
if necessary, loading the classes referred to.
2.
Initialization Initializing the superclass (if necessary, including loading and linking it), then executing
all the static initializers and static initialization blocks of the class.
3.
The Class object returned by defineClass only represents a loaded classit has not yet been linked. You
can explicitly link by invoking the (misnamed) resolveClass method:
protected final voidresolveClass(Class<?> c)
Links the specified class if it has not already been linked.
The loadClass method we described does not resolve the class that is loaded. A protected, overloaded
form of loadClass takes an additional boolean flag indicating whether or not to resolve the class before
returning. The virtual machine will ensure that a class is resolved (linked) before it is initialized.
A class must be initialized immediately before the first occurrence of: an instance of the class being created; a
static method of the class being invoked; or a non-final static field of the class being accessed. This includes
the use of reflection methods to perform those actions. Additionally, before an assert statement is executed
inside a nested type, the enclosing top-level class (if there is one) must be initialized. Using reflection to
directly load a class may also trigger initializationsuch as use of Class.forName(name)but note that
simple use of a class literal does not trigger initialization.
16.13.3. Loading Related Resources
Classes are the primary resources a program needs, but some classes need other associated resources, such as
text, images, or sounds. Class loaders have ways to find class resources, and they can use the same
mechanisms to get arbitrary resources stored with the class. In our game example, a particular playing strategy
might have an associated "book" that tells it how to respond to particular situations. The following code, in a
BoldPlayer class, would get an InputStream for such a book:
String book = "BoldPlayer.book";
InputStream in;
ClassLoader loader = this.getClass().getClassLoader();
if (loader != null)
in = loader.getResourceAsStream(book);
else
in = ClassLoader.getSystemResourceAsStream(book);
System resources are associated with system classes (the classes that may have no associated class loader
instance). The static ClassLoader method getSystemResourceAsStream returns an
InputStream for a named resource. The preceding code checks to see whether it has a class loader. If it
does not, the class has been loaded by the bootstrap class loader; otherwise, it uses the class loader's
getresourceAsStream method to turn its resource name into a byte input stream. The resource methods
return null if the resource is not found.
The Class class provides a getresourceAsStream method to simplify getting resources from a class's
class loader. The preceding code could be written more simply as
String book = "BoldPlayer.book";
InputStream in = BoldPlayer.class.getResourceAsStream(book);