Implementation of Java dynamic class loading mechanism

Source: Internet
Author: User
Tags static class
-->
-->As a best example of a dynamic class loading mechanism that takes full advantage of Java, a Web browser with Java extensions dynamically loads Java applets from a network or local file system on request (following a A regular Java small application class, and then executes it in the local system, greatly enhancing the functionality of the home page.

----In fact, JAVA itself is a very dynamic language. Windows-like dynamic-link libraries (DLLs), Java applications are always compiled into separate class files, which are dynamically loaded by the Java virtual machine as needed when the program executes. This mechanism makes it possible to write dynamic distributed applications: we can write our own class loader on the client, while the program actually executes is stored locally , a local area network, or a host on the other end of the world. The following describes how to implement the Java dynamic class loading mechanism in your own application.

System classes loaded with dynamic classes
----to support the dynamic class loading mechanism, two classes are provided in the System class group Java.lang: Class class and ClassLoader class.

----1, class JAVA.lang.Class. In a Java virtual machine, each class or interface is manipulated by the class class, it cannot be explicitly instantiated, and other methods must be used to get the object of the class. A key step in the dynamic class loading mechanism is how to get objects of the class type of the specified classes. The related methods include:

----public static Class forname (String className)

----This is a static method that gets the class type object of the specified name, and the class name can be a string like "Sun.applet.Applet", but not with a path or a network Address and other information. This is the most convenient way to dynamically load classes from the local system.

----Public Object newinstance ()

----This is the most important method, which establishes an instance of the specified class as described by the class type object.

----Below is a code that implements dynamic class loading using the Forname () and Newinstance () methods, and the share class contains an interface that will be explained in part three.

try{
Creates an object of class type based on the class name.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.