Objective
In many traditional languages, the program is immediately loaded as part of the startup process. Then it is initialized, and then the program starts running. The initialization of these languages must be carefully controlled to ensure that they are defined as
StaticThings, whose initialization order does not cause trouble. For example
C + +, if a
StaticExpect another
StaticIt can be used effectively before it is initialized, and then there is a problem.
JavaThis problem does not occur because it takes a different load mode. Loading is one of the many things that become easier because
JavaAll things in the object are objects. Keep in mind that the compiled code for each class exists in their own separate files. The file is loaded only when the program code is required. In general, it is possible to say: "The code for the class is loaded at first use." "This usually means that loading occurs when the first object of the class is created, but when accessing the
StaticDomain or
Staticmethod, the load also occurs. The first place to use is also
StaticWhere the initialization occurs. All the
StaticObjects, and
StaticCode snippets are initialized sequentially at load time, in the order in which they are defined (that is, when the class is written). Of course, defined as
StaticThings will only be initialized once.
Sample source Code
It is beneficial to understand the whole process of initialization, including inheritance, in order to have a global grasp of what is happening. Take a look at the following example:
Base class
Package Com.mufeng.theseventhchapter;public class Insect {private int i = 9;protected int j;public insect () {SYSTEM.OUT.PR Intln ("i =" + i + ", j =" + J "); j=39;} private static int x1 = Printinit ("Static insect.x1 initialized"); static int printinit (String s) {System.out.println (s); r Eturn 47;}}
Export class
Package Com.mufeng.theseventhchapter;public class Beetle extends insect {private int k = Printinit ("beetle.k initialized" );p ublic Beetle () {System.out.println ("k =" + K + ", j =" + j);} private static int x2 = Printinit ("Static beetle.x2 initialized");p ublic static void Main (string[] args) {System.out.print ln ("Beetle constructor"); Beetle B = new Beetle ();}}
Output results
Static insect.x1 initializedstatic beetle.x2 initializedbeetle Constructori = 9, j = 0beetle.k Initializedk =, j = 39
SOURCE parsing
In
BeetleRunning on
Java, the first thing that happens is trying to access
Beetle.main ()(A
Staticmethod), the loader starts to start and finds
BeetleThe compilation code for the class (in the name
Beetle.classfiles). As you load it, the compiler notices that it has a base class (which is determined by the keyword
extendsLearned), so it continues to load. This happens regardless of whether you intend to produce an object of that base class. If the base class has its own base class, then the second base class is loaded, and so on. Next, the basic class of
StaticInitialization (in this case, the
Insectis executed, then the next export class, and so on. This approach is important because the export class's
StaticInitialization may depend on whether the base class member can be initialized correctly. So far, the necessary classes have been loaded and the objects can be created. First, all the basic types in the object are set to the default values, and the object references are set to
NULL----This is generated at a stroke by setting the object memory to a binary 0 value. The constructor for the base class is then called. In this case, it is automatically called. But it can also be used
SuperTo specify a call to the base class constructor (as
Beetle ()The first action in the constructor). The base class constructor, like the constructor for the exported class, undergoes the same procedure in the same order. After the base class constructor is complete, the instance variables are initialized in their order. Finally, the rest of the constructor is executed.
Includes initialization of inherited classes and loading of classes one (attached source)