java new object a process of what happened?

In a new java object when the class will first see the object belongs has not been loaded into memory, if not, it will first be loaded via the fully qualified name of the class. After the completion of the class is loaded and initialized, and then create work objects.

Our first assumption is that the first use of this type, in which case a new object can be divided into two processes: loads and initializes the class and create objects.

First, the class loading procedure (first class using)

java is to use the parent delegation model to load the class, so the class before describing the loading process, we look at its work process:

Parents entrust the work process model is: if a class loader (ClassLoader) received a request class is loaded, it first does not own to try to load this class, but to entrust the request to the parent class loader to complete each level class loader is true, so all requests ultimate load should be transferred to the top of the boot class loader, only when the parent class loader to load the feedback they can not complete the request (in its search did not find what you need when loaded class), sub-loader will try to load their own

The benefits of using parents entrust mechanism: the ability to effectively ensure global uniqueness of a class, when the same class name appears more limited program, the class loader when performing loading, always will load only one of these classes.

1, Load

A class loader is responsible for the reading of such binary byte flows inside the JVM according to a fully qualified name of the class, and storing method of run-time memory area in the zone, and then convert it to a corresponding type of target java. lang.Class object instance

2, verification

Format Verification: Verify whether the class file specification

Semantic validation: checking the type is marked as a final subclass is included; final method checks whether a class subclasses overwriting; No statement methods to ensure compatibility between the parent and child classes (such as the same method signature , but different from the return value)

Operation verification: data operand stack must be correct operation of the various symbols in the constant pool perform verification (usually performed at the reference resolution phase, check whether the fully qualified name to specify the type described can be positioned by reference symbol on, as well as access modifier class members whether to allow access to information, etc.)

3, ready

All assigned to static variables in the class memory space, and to set an initial value (since the object has no instance variables are not within the scope of this operation)

The modified final static variables (constants), will direct assignment;

4, parsing

Reference symbol the constant pool into direct reference (obtained class or field, or a method pointer offset in the memory, in order to invoke the direct method), then this may be performed after the initialization.

Parses the content needs to be statically bound. // all methods will not be rewritten and will be static binding domain

2,3,4 or more stages and three link stages collectively, do link stage is loaded into the JVM in the class of binary data byte stream is incorporated into the JVM operating state.

5, initialization (Fathers rear sub)

5.1 assign a static variable

Performing static code block 5.2

Note: static block of code can be invoked only jvm

If multiple threads need to initialize a class at the same time, only one thread can only allow them to perform initialization, the thread must wait for the rest, only after active thread executing the initialization of the class, will notify other threads waiting.

Because of dependency on the parent subclass, so that the loading order to load the class parent class loader subclass, the same initialization. However, when the parent class initialization, the value of the static variable sub-class also has some, it is the default.

Finally, the method area stores the current class class information, including static class variables, class initialization code (assignment statement when the definition static variables and initialization code blocks), the instance variables defined instance initialization code (assignment statement when the definition of instance variables examples of such information and block construction methods) and instance methods, as well as a reference to the parent class.

Second, create an object

1, the object needs to allocate memory in the heap

Memory allocation includes all the instance variables in this class and the parent class, but does not include any static variable

2, assign default values ​​for all instance variables

The method defined within the copy of the instance variables to a heap, and then assign the default value

3, instance initialization code executed initialization sequence is initialized parent reinitialization subclass, execute the example code block is then initialized when the constructor

4, if there are similar Child c = new Child () in the form of c-quoted definition of type Child reference variable c, and assigning the address to which the object heap area in the stack area

Note that each subclass of parent class object holds references to objects, can be called by the parent object inside the super keyword, but is not accessible externally

supplement:

Call the instance method by reference example, when the actual type of information start with the method of the object in the find, can not find the words to go looking for parent class type information.

If the inheritance hierarchy is deep, the method to call the parent class is located in the upper comparison, the efficiency of the call is relatively low, because every call to go through many times to find. Most of this time the system will use a method called virtual method table to optimize the efficiency of the call.

The so-called virtual method table, that is, when the class is loaded, create a class for each table, this table includes all methods and their addresses dynamic binding of objects of the class, including the parent class, but a method is only one record, after subclass overrides a parent class method will only save subclass. When an object by dynamic binding method, just look up the table on it, without the need to find one by one for each parent.

Guess you like

Origin www.cnblogs.com/userpu/p/12197534.html