JAVA class loading mechanism

Source: Internet
Author: User

First, you need to know how to use the class loading mechanism?

First, it can control JVM behaviors. Second, it can allow programs to dynamically control class loading, such as hot deployment, to improve program flexibility and adaptability.

Class is loaded. Compile the Java file. Class file, and then load it To the memory.

When it comes to class loading, we have to say the classpath used to configure jdk environment variables. The general configuration order is :.; d: \ Java \ jdk6 \ lib \ dt. jar; D: \ Java \ jdk6 \ lib \ tools. jar. If you change the order, the order of loading will also change.

JAVA has three types of loaders:

1. Bootstrap Loader (start class Loader): load the path or jar specified by System. getProperty ("sun. boot. class. path. 2. Extended Loader (ExtClassLoader): loads the path or jar specified by System. getProperty ("java. ext. dirs. When running a program using Java, you can also specify the search path, for example, java-Djava. ext. dirs = d:/projects/testproj/classes HelloWorld3, AppClass Loader (System class Loader AppClassLoader): load System. getProperty ("java. class. path ") specifies the path or jar. When running a program in Java, you can also add-cp to overwrite the original Classpath settings, for example, java-cp. /lavasoft/classes HelloWorldExtClassLoader and AppClassLoader will save a copy in JVM after JVM is started, and cannot change the search path during the program running. If you want to load classes from other search paths at runtime, a new class loader will be generated.

During JAVA loading, it is loaded from top to bottom. Note that Bootstrap Loader is the top class Loader and its parent Loader is null. however, ext and boot are not parent-child relationships. Because boot is implemented in C.

Let's look at an example:

public static void main(String[] args){System.out.println(AAA.class.getName());System.out.println(AAA.class.getClassLoader());System.out.println(AAA.class.getClassLoader().getParent());System.out.println(AAA.class.getClassLoader().getParent().getParent());System.out.println(String.class.getClassLoader());}

Output result:

com.AAAsun.misc.Launcher$AppClassLoader@18efd7csun.misc.Launcher$ExtClassLoader@a40f0enullnull

Class Loading

There are three methods for Class loading: 1. When the command line starts an application, it is initialized and loaded by JVM. 2. It is loaded by Class. the forName () method is dynamically loaded. 3. The ClassLoader is used. the loadClass () method has a big difference in dynamic loading. Let's take a look at the example to understand: public class HelloWorld {
Public staticvoid main (String [] args)
Throws ClassNotFoundException {
ClassLoader loader = HelloWorld. class. getClassLoader ();
System. out. println (loader );
// Use ClassLoader. loadClass () to load the class. Initialization blocks are not executed.
Loader. loadClass ("Test2 ");
// Use Class. forName () to load the Class. The initialization block is executed by default.

// Class. forName ("Test2 ");
// Use Class. forName () to load the Class and specify ClassLoader. No static block is executed during initialization.
// Class. forname ("Test2", false, loader );
}
} Public class Test2 {
Static {
System. Out. println ("the static initialization block is executed! ");
}
} The loading method is switched separately and different output results are displayed.

Classloader is also important: the class file loaded by the same classloader has only one class instance. However, if the same class file is loaded by different classloader, there will be two different classloader instances (provided that the two class loaders cannot use the same parent class loaders ).

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.