Java Reference
In-Depth Information
player.play(game);
game.reportScore(name);
} catch (Exception e) {
reportException(name, e);
}
}
}
// ... 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 ap-
plication. If two classes have different class loaders then they are dis-
tinct 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 vari-
ables and modifications to the static variables of one class have no ef-
fect 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 cre-
ation; 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