Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Dynamic Class Loading

25% developed
From Wikibooks, open books for an open world
<Java Programming |Reflection

NavigateReflection topic:()

Dynamic Class Loading allows the loading of java code that is not known about before a program starts. Many classes rely on other classes and resources such as icons which make loading a single class unfeasible. For this reason theClassLoader (java.lang.ClassLoader) is used to manage all the inner dependencies of a collection of classes. The Java model loads classes as needed and doesn't need to know the name of all classes in a collection before any one of its classes can be loaded and run.

Simple Dynamic Class Loading

[edit |edit source]

An easy way to dynamically load aClass is via thejava.net.URLClassLoader class. This class can be used to load aClass or a collection of classes that are accessible via a URL. This is very similar to the-classpath parameter in thejava executable. To create aURLClassLoader, use the factory method (as using the constructor requires a security privilege):

ExampleCode section 10.4: Class loader.
URLClassLoaderclassLoader=URLClassLoader.newInstance(newURL[]{"http://example.com/javaClasses.jar"});

Unlike other dynamic class loading techniques, this can be used even without security permission provided the classes come from the same Web domain as the caller.Once aClassLoader instance is obtained, a class can be loaded via theloadClass method. For example, to load the classcom.example.MyClass, one would:

ExampleCode section 10.5: Class loading.
Class<?>clazz=classLoader.load("com.example.MyClass");

Executing code from aClass instance is explained in theDynamic Invocation chapter.


Clipboard

To do:
Add some exercises like the ones inVariables

Retrieved from "https://en.wikibooks.org/w/index.php?title=Java_Programming/Reflection/Dynamic_Class_Loading&oldid=3644578"
Category:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp