Current location - Education and Training Encyclopedia - Education and training - Java Training of Beida Jade Bird: Two Ways to Create New Objects?
Java Training of Beida Jade Bird: Two Ways to Create New Objects?
With the development of Internet programming technology, the programming language has changed from program-oriented to object-oriented.

Today, we will learn how to create new objects in the java programming language from two aspects.

When java is in a new object, it will first check whether the class to which the object belongs has been loaded into memory. If not, it will first be loaded through the fully qualified name of the class.

After loading and initializing the class, the object will be created.

Let's assume that the class is used once, so that an object can be divided into two processes: loading and initializing the class and creating the object.

First, the class loading process (using this class once) java uses the parent delegation model to load classes, so before describing the class loading process, let's take a look at its working process: the working process of the parent delegation model is that if a class loader receives a class loading request, it will not try to load the class itself, but entrust the request to the parent class loader to complete it. This is true for class loaders at every level, so all loading requests should eventually be sent to the top-level startup class loader. Only when the parent class loader feedbacks that the loading request cannot be completed (no class to be loaded is found in its search range), the subclass loader will try to load itself.

The advantage of using parent delegation mechanism is that it can effectively guarantee the global nature of a class. When there are multiple classes with the same qualified name in the program, the class loader always loads only one of them when loading.

The 1. class loader is responsible for reading the binary bytes of the class according to the fully qualified name of the class, storing them in the method area of the runtime memory area, and then converting them into java.lang.Class object instances corresponding to the target type. 2. Verification format verification: verifying whether it conforms to the class file specification semantic verification: checking whether the type marked as final contains subclasses; Check whether the final method in the class is overridden by a subclass; Ensure that there are no incompatible method declarations between the parent class and the subclass (for example, the method signatures are the same, but the return values of the methods are different). Operation verification: the data in the operand stack must be operated correctly, and all kinds of symbol references in the constant pool should be verified (usually, in the parsing stage, it is checked whether the fully qualified names described in the symbol references can be located on the specified type, whether the access modifiers of the class member information allow access, etc.). 3. Prepare to allocate memory space for all static variables in the class, and set an initial value for them (because the object has not yet been generated, and the instance variable is not within the scope of this operation), and the static variables (constants) modified by final will be directly assigned; 4. Resolve the symbol references in the constant pool into direct references (get pointers or offsets of classes or fields and methods in memory to directly call methods), and then execute them after initialization.

Parse content that requires static binding.

//All methods and fields that will not be rewritten will be statically bound. The above three stages are also called the link stage. What needs to be done in the link stage is to merge the class data information of binary byte stream loaded into JVM into the runtime state of JVM.

5. Initialization (parent before child) 4. 1 Assigning values to static variables 4.2 Executing static code blocks Note: Static code blocks can only be called by jvm. If a class needs to be initialized by multiple threads at the same time, only one thread can be allowed to initialize it, and other threads must wait. Only after the active thread finishes initializing the class will other waiting threads be notified.

Because the subclass depends on the parent class, the loading order of the class is to load the parent class first, then load the subclass, and so is initialization.

However, when the parent class is initialized, the value of the static variable of the subclass is also the default value.

Finally, the method area will store the class information of the current class, including class static variable, class initialization code (assignment statement and static initialization code block when defining static variable), instance variable definition, instance initialization code (assignment statement instance code block and construction method when defining instance variable) and instance method, as well as the class information reference of the parent class.

2. Create the object 1. Allocate the memory needed by the object in the heap area. The allocated memory includes all instance variables of the class and the parent class, but does not include any static variables. 2. Assign default values to all instance variables, copy the definitions of instance variables in the method area to the heap area, and then assign default values. 3. The initialization sequence of executing instance initialization code is to initialize the parent class first, and then initialize the subclass. When initializing, the instance code block is executed first, and then the constructor is executed. 4. If there is a C reference in the form of Childc=newChild (), define the subtype reference variable C in the stack area, and then assign the address of the heap area object to it. It should be noted that every subclass object in Liaoning IT Training/Discovery holds a reference to the parent class object, which can be called internally by super keyword and inaccessible externally.