Class initialization and initialization

Source: Internet
Author: User

Class initialization and initialization
Private static void parseClassToLoad (XmlPullParser parser) throws Exception {
String className = parser. nextText ();
// Attempt to load the class so that the class can get initialized
Try {
Class. forName (className );
}
Catch (ClassNotFoundException cnfe ){
System. err. println ("Error! A startup class specified in smack-config.xml cocould "+
"Not be loaded:" + className );
}
}
JAVA initialization and class loading

It is unlikely that you fully use code to prove the actions of class loading, because before you use this class (that is, when you want to start to prove the process of class loading), she has loaded.

For now, static members are initialized together with the class loading, so a maximum of static members can be used to prove the class loading.

The problem is how to prompt messages when static members are initialized. There are two methods:

The first is to use static field members. This member is designated as an object initialization and will be notified in the constructor of this initialization object. Code:
Public class StaticLoad {
Public static loadpolicy = new loadpolicy ();
}

Public class loadpolicy {
Public loadpolicy (){
System. out. println ("Class is Loaded ");
}
}
When the StaticLoad Class is used, static members are initialized when the Class is Loaded to prove that the Class is Loaded, and only one call is made once.

The second method is to use static blocks. The Code is as follows:
Public class StaticLoad {
Public static loadpolicy = new loadpolicy ();
Static {
System. out. println ("Class is Loaded ");
}
}
When the class is loaded, the code in the static block is also executed.

This is the method for notification when the class is loaded. You can write the code yourself and they will only play it once.

For the loading sequence of classes, there are detailed information on the Internet. Here I will only talk about some simple:
First, you need to use a class to read such code from classpath to the code storage area of the memory class, and then open a storage area for each static field, then store the method code. Next, initialize the class. All static members are initialized in sequence (from top to bottom in the Code Declaration Order). When other classes need to be loaded, load other classes. Then complete the initialization for the virtual machine.
When instantiating a class, if it needs to be loaded, it will be loaded in the above order, and then allocate the space of all instance fields in the memory, and then call the constructor, the constructor calls the parent Constructor (displayed or implicitly) and executes the remaining constructor code. Finally, return the reference of the instantiated object and return the constructor.

When is the static internal class field initialized in java? Is it initialized when the external class is loaded?

No. Initialization is performed only when the internal class is loaded. // you can continue communication if any problem occurs.
The landlord only needs to understand the code
So the loading of Classes
Basically, no problem.
Explanation of the last class loading process
Public class Test6 {
Public static void main (String [] args ){
New B ();
New A. C ();
}
}

Class {
Private P p1 = new P ("A -- p1 ");
Static P p3 = new P ("A -- p3 ");
Public (){
System. out. println ("()");
}
Private P p2 = new P ("A -- p2 ");
Static {
New P ("A -- static ");
}
{New P ("{...}");}

Public static class C {
Private P p1 = new P ("C -- p1 ");
Static P p3 = new P ("C -- p3 ");
Public C (){
System. out. println ("C ()");
}
Private P p2 = new P ("C -- p2 ");
Static {
New P ("C -- static ");
}
{New P ("C {...}");}
}
}

Class B extends {
Private P p1 = new P ("B -- p1 ");
Static P p3 = new P ("B -- p3 ");
Public B (){
System. out. println ("B ()");
}
Public P p2 = new P ("B -- p2 ");
Static {
New P ("B -- static ");
}

{New P ("B {...}");}
}

Class P {
Public P (String s ){
System. out. println (s );
}
}
/*

Parent class
1. Load the parent class
1.1 allocate storage space for static attributes and assign initial values
1.2 execute static initialization block and static initialization Statement (from top to bottom)
2. Load subclass
2.1 allocate storage space for Static Properties
2.2 execute static initialization block and static initialization Statement (from top to bottom)
3. Load the parent class Constructor
3.1 allocate storage space for instance attributes and assign the Initial Value
3.2 execute the instance initialization block and instance initialization statement
3.3 execute the constructor content
4. Load the subclass Constructor
4.1 allocate storage space for instance attributes and assign the Initial Value
4.2 execute the instance initialization block and instance initialization statement
4.3 execute the constructor content
... The remaining full text>

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.