Java class loader _01

Source: Internet
Author: User

1. Concept:

As the name implies, the ClassLoader (class loader) is used to load Java classes into a Java virtual machine. In general, Java virtual machines use Java classes in the following way: Java source programs (. java files) are converted to Java bytecode (. class files) after being compiled by the Java compiler. The ClassLoader is responsible for reading the Java byte code and converting it into java.lang.Class an instance of the class. Each such instance is used to represent a Java class. newInstance()an object of the class can be created by means of this instance. The actual situation can be more complex, such as the Java byte code may be generated dynamically through the tool, or it may be downloaded over the network.

2.

  java.lang.ClassLoaderThe basic function of a class is to find or generate its corresponding byte code based on the name of a specified class, and then define a Java class from those byte codes, which is java.lang.Class an instance of the class. In addition, it ClassLoader is also responsible for loading the resources required for Java applications, such as files and configuration files.

  Class.forNameis a static method that can also be used to load classes. There are two forms of the method: Class.forName(String name, boolean initialize, ClassLoader loader) and Class.forName(String className) . The first form of the parameter represents the full name of the class, whether the class is initialized, or the ClassLoader that is name initialize loader used when loading. The second form is equivalent to setting the value of the parameter to initialize true loader the class loader of the current class. Class.forNamea very common usage is when the database driver is loaded. such as Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance() the driver used to load Apache Derby database.

3. Code:

  

//  on Method[] mds = methodutils. class . GetMethods (); //  Geneva Class clas = Class.forName ("com.zilanghuo.loader.MethodUtils"= Clas.newinstance ();

4. Call the Invoke method:

 

// obj: object instance, args: Method parameter value, return value is the return value of the method Object      This Method object, on the specified object with the specified parameters.

Java class loader _01

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.