}
}
// ... definition of other methods ...
}
Each new game creates a new PlayerLoader object to load the class for that run of the game. The new
loader loads the class, using loadClass, returning the Class object that represents it. That Class object
is used to create a new object of the Player class. Then we create a new game and play it. When the game
is finished, the score is reported. Without a new class loader for each run, attempting to load a class with the
same name as one that had already been loaded would return the original class. This would prevent players
from submitting updated versions of their classes with the same name.
You can obtain the class loader for a given Class object from its getClassLoader method. System
classes need not have a class loader, so the method may return null.
Class loaders define namespaces that separate the classes within an application. If two classes have different
class loaders then they are distinct classes, even if the binary data for the class was read from the same class
file. Each distinct class maintains its own set of static variables and modifications to the static variables of one
class have no effect on the other class.
Each thread has an associated ClassLoader that will be used by default to load classes. This context class
loader can be specified at thread creation; if none is specified the parent thread's context class loader will be
used. The context class loader of the first thread is typically the class loader used to load the applicationthe
system class loader. The Thread methods getContextClassLoader and
setContextClassLoader allow you to get and set the context class loader.
16.13.1. The ClassLoader Class
A class loader can delegate responsibility for loading a class to its parent class loader. The parent class loader
can be specified as a constructor argument to ClassLoader:
protectedClassLoader()
Creates a class loader with an implicit parent class loader of the system class
loader, as returned by getSystemClassLoader.
protectedClassLoader(ClassLoader parent)
Creates a class loader with the specified parent class loader.
A generic class loader, intended for use by others, should provide both forms of constructor to allow an
explicit parent to be set.
The system class loader is typically the class loader used by the virtual machine to load the initial application
class. You can get a reference to it from the static method getSystemClassLoader.
The bootstrap class loader is the class loader used to load those classes belonging to the virtual machine
(classes like Object, String, List, and so forth). These classes are often referred to as system classes, a
term which can lead to some confusion since the system classes are loaded by the bootstrap loader, whereas
application classes are loaded by the system class loader. The bootstrap loader may or may not be represented
by an actual ClassLoader object, so invoking getClassLoader on an instance of one of the system