Java-the full process of initialization

Source: Internet
Author: User

Since everything in Java is an object, many actions become easier. Loading actions is only one of them.
The compilation code of each class in Java exists in its own independent file. this file is loaded only when the program code is required. it can be said: "The class code is loaded for the first time ". this usually means that the class is loaded only when the first object of the class is built. However, when the static field method is accessed, it will also load.
Static Initialization is also used for the first time. all static objects and static code segments are initialized in the order of the Program (that is, the writing order of the class) during loading. of course, things defined as static will only be initialized once.

/***//**
* Title: the full process of initialization <br>
* Description: describes the entire initialization process, including inheritance, to have a global grasp of everything that happens. <br>
* Copyright: (c) 2008 thinking in Java <br>
* Company: Augmentum Inc <br>
* @ Author forest he
* @ Version 1.0
*/
Class insect ...{
Private int I = 9;
Protected Int J;
Insect ()...{
System. Out. println ("I =" + I + ", j =" + J );
J = 37;
}
Private Static int X1 = Print ("static insect. x1.initialized ");
Public static int print (string s )...{
System. Out. println (s );
Return 47;
}
}

Class bettle extends insect ...{
Private int K = Print ("bettle. K initialized ");
Public bettle ()...{
System. Out. println ("k =" + k );
System. Out. println ("J =" + J );
}
Private Static int X2 = Print ("static bettle. X2 initialized ");
Public static void main (string [] ARGs )...{
System. Out. println ("bettle constructor ");
Bettle B = new bettle ();
}
}

In the above example, when Java is run on beetle, the first thing that happens is to try to access beetle. main () (a static method), so the loader starts to start and find the compilation code of the beetle class (it is compiled into a code named beetle. class file ). during beetle loading, the compiler notices that it has a base class (this is known by the extends keyword), so beetle continues to load (loading the base class ). whether or not you want to generate an object of the base class, this will happen.
If the base class has its own base class, the second base class will be loaded, and so on. next, the static initialization in the base class (insect in this example) will be executed, followed by the next export class, and so on. this method is very important because the static initialization of the export class may depend on whether the base class members can be correctly initialized.

So far, all necessary classes have been loaded, and the object can be created. first, all the basic types and objects in the base class object will be set as the default value. Then, the base class constructor will be called (automatically called or manually called through Super ). after the basic class constructor is complete, the export class members are initialized in order, and the export class constructor is executed.

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.