Java Dynamic loading mechanism

Source: Internet
Author: User

Suppose that there is a class,classloader first load it into the memory of the code segment (memory stored in the snippet), standing in the angle of ClassLoader, the memory of a class is one of the objects, This object is Xx.class, which is actually the object of class. After load class, find the main function to start execution, and then put a lot of other classes load in, dynamic loading mechanism.

To test the dynamic loading mechanism:

New Project Reflection,new a class,testdynamicloading:

 Public classtestdynamicloading { Public Static voidMain (string[] args) {NewA (); System.out.println ("*****-------------------------******"); NewB (); NewC (); NewC (); NewD (); NewD (); }}classa{}classb{}classc{//Static statement block, called only once.     Static{System.out.println ("CCCCCCCCCCCCCCCCCCCCCCCCCCCC"); }    }classd{//A dynamic statement block that is called once every new object is created. Not much to use. {System.out.println ("DDDDDDDDDDDDDDDD"); }}

Right-click Run as-----> Run configurations.

Program arguments: Command line parameters

VM arguments: The parameters of the virtual machine, the Java program runs when the Java command line gives you the specified virtual machine parameters

-verbose:class: Verbose output.

---> Run

You can see the output of the console, and the steps to perform the program are:

1, first load class A,

2, System.out.println ("* * * * * * * *-------------------------******");

3, Load Class B,

4, Load Class C, and the static statement block executes only once.

5, Load Class D, dynamic statement block executed 2 times.

The dynamic loading mechanism of Java is the same and is verified.

[Loaded A from file:/d:/eclipse/gongzuokongjian/reflection/bin/]* * * * *-------------------------******  [Loaded B from File:/d:/eclipse/gongzuokongjian/reflection/bin/][loaded C from file:/d:/eclipse/ gongzuokongjian/reflection/bin/]cccccccccccccccccccccccccccc[loaded D from file:/d:/eclipse/ gongzuokongjian/reflection/bin/]dddddddddddddddddddddddddddddddd

Java Dynamic loading mechanism

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.