Classload and Class.forName () differences

Source: Internet
Author: User

Transferred from: http://carl-java.iteye.com/blog/978680

Both Class.forName and ClassLoader in Java can be used to load classes. The former, in addition to loading the class. class file into the JVM, also interprets the class and executes the static blocks in the class. and ClassLoader only do one thing, is to load the. class file into the JVM, do not execute the contents of static, only the newinstance will execute the static block. Class.forName (name, initialize, loader) with the parameter function also controls whether static blocks are loaded. And only call the Newinstance () method using the call constructor to create the object of the class
The case code is as follows:
1. Loading with ClassLoader

System.out.println ("Before loadclass ... ");
Class c =test.class.getclassloader (). LoadClass ("Com.hundsun.test.ClassInfo");
System.out.println ("After loadclass ... ");
System.out.println ("Before newinstance ... ");
ClassInfo info1 = (ClassInfo) c.newinstance ();
System.out.println ("After newinstance ... ");

Output Result:
Before LoadClass ...
After LoadClass ...
Before newinstance ...
Static invoked ...
Contruct invoked ...
After newinstance ...

2. Loading with Class.forName

System.out.println ("before Class.forName");
Class cc =class.forname ("Com.hundsun.test.ClassInfo");
System.out.println ("after Class.forName");
ClassInfo Info2 = (ClassInfo) cc.newinstance ();
Output Result:
Before Class.forName
Static invoked ...
After Class.forName
Before newinstance ...
Contruct invoked ...
After newinstance ...

Let's take a look at the two specific procedures.
The LoadClass () method loads the class and initializes the process:
Class Load (LoadClass ()) (load)--"newinstance () (link + initialize)
Newinstance ():
(Start connecting) static code block--"General variable allocation preparation (A=0;b=0;c=null)-" (Start initialization) common variable assignment (a=1;b=2;c= "haha")--"construction Method--" was initialized successfully.


Class.forName (stirng className) A parameter method to load the class and initialize the process:
Class Load (Class.forName ()) (load)--"Static code block--" newinstance () (link + initialize)

Newinstance ():
(Start connection) general variable allocation Preparation (A=0;b=0;c=null)-"(Start initialization)" Normal variable assignment (a=1;b=2;c= "haha")--"construction Method--" was initialized successfully.


Class.forName () Three parameters of the loading class and initialization process is the same as ClassLoader.

From the top of the breakpoint debugging can be seen that the static code block is not completed in the initialization phase, it is stuck in class initialization, before the normal variable default allocation (integer allocation is 0, the string is assigned to null), which is why we can not reference the ordinary variable in the static code block one of the reasons, which is the so-called "allocation" "Initialization" is a violation of the.

So I think the class loading and initialization process of the JVM should be like this.

1. Class load: Bootstrap loader--"Extended loader--" System Loader
2. Static code block initialization
3. Links:
A) Verify that the Java specification is compliant
b) Preparation: Default initial value
c) Parse: symbol reference to direct reference, resolve address
4. Initialization
A) Assignment: the initial value in Java code
b) Construct: constructor

(turn) Classload and class.forname () differences

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.