Java-Preliminary Understanding-tenth-object initialization process

Source: Internet
Author: User

One.

Now simply string the memory diagram in a string, from the previous to the present all together to tell,

The result of the compilation run is 9, according to our previous understanding, the object is initialized by default, then the initialization is displayed, and finally the constructor is initialized, which is seen from our DOS results (if the constructor is initialized before, the output is 0).

At the time of inheritance, this order has been clarified.

To clarify, that is the case,

The first line of the compilation is 0, and when we create the object, after the member variable num is initialized by default, the show () method should print 9 instead of 0 if it is followed by the num=9 initialization, which shows that the initialization is not doing so fast. The result of the compilation is this, explained directly, after inheriting, there is an implicit super () in the subclass; here, after the object is initialized by default on the member variable, the constructor of the subclass is followed. The constructor of the subclass encounters Super (), then the constructor of the parent class, and the method body of the constructor of the parent class is the show () method. The show () method is the subclass in the call, walking is the method after the overwrite, so printed is the subclass of the Show () method of the body ( why do the parent class method, the operation is the quilt class overrides method ?). At this point, Num is not displayed initialized, so num=0 is printed. Therefore, the display initialization is after the parent class constructor.

Why is Num 9 in the constructor of the subclass? Description after Super (), there is a display initialization. This problem, we have already solved ( why now and forget, explain or focus on how to operate, but how to come .) Understand how to come, also understand how to operate. )

Essentially, the constructor executes first, and then the display initialization is inserted. However, it is undeniable that the constructor appears before the display initialization. (You know, we're a new object, and the construction method is in the stack.) how in the hell did you start the display initialization in the constructor ? )

In the early days we were talking, it was like the constructor was executed.

Two.

Next, make a change to the program to show the initialization process in different situations.

In the program, there is a construction code block, so how to initialize.

The compilation results show that the constructor precedes the constructor. As a result, we can conclude that constructing a block of code is performed before the constructor. Is this really true?

( how can num be 9 when constructing a block of code ?) )

The following extends the program (complements the child parent class to tell the whole process) to tell the entire initialization process.

The "hehe" in the parent class show () method does not matter, it does not run because it covers the quilt class.

(Judging from the program in the main function, there is only one sentence to create the object.) The result of the compilation run is all that occurs during the creation of the object)

The process of initialization is explained by the program in which there is only one creation object statement in the method body of the main function, new Zi ();

The results of the compilation run are as follows, carefully analyzing each step of the process through the results.

Explaining the memory diagram can not be separated from the CPU internal structure, stack, heap and method area, three parts.

A new child object, the child class is loaded, and the child parent class is loaded in. This is an inheritance system, so it is not just loading subclasses, but joining the parent class together into the method area.

The whole process is this, you need to create a subclass object, because the object is created subclass, so you need to load the subclass into the method area, and this is an inheritance system, so not only the subclass into the method area, the parent class to enter the method area. The code in the child parent class is loaded into the method area, and the method area is the storage function. These programs are stored first, and then the code for these method areas is called on the stack and in the heap.

The process of creating an object is now the loading child parent class, then opening up space in the heap, loading member variables, and initializing the values by default. All of the next steps are in the initialization process. First, the subclass constructor is loaded into the stack, and a sub-class constructor is super (); This is the call to the parent class constructor, the parent class has two sentences, the first sentence is not involved in any member, simply output a statement. The second sentence is to use the show () method ( why the Show () method used in the parent class constructor is not the show () of the parent class, but the show () method of the child class ? The answer to the video is that it's all a subclass in the call, which is the premise, so call the show () method, first look at the subclass there is no, there is a show () method in the subclass, so use your own show () method. ), when calling the subclass Show () method, you need to output num, the subclass object has num, and only the default initialization, so that the output is num=0. Everything above is the super () statement in the subclass constructor, where the initialization steps are labeled, Super (), show initialization, construct block initialization, and initialize the remaining subclass constructors.

Using the following example to recall the initialization process,

In this example, when creating an object, the child parent class has NUM, and two num is saved into the subclass object.

Java-Preliminary Understanding-tenth-object initialization process

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.