Why does the constructor for a Java constructor only write this () or super () in the first line?

Source: Internet
Author: User

Recently in the internal class, but was "why anonymous internal class or local internal class use method of local variables, local variables must be final type" troubled, when looking for information on the Internet, I found that class initialization completely do not understand, before the understanding is wrong! So looking back at the basic initialization, when looking at the initialization, it was found that "why only the first line of the constructor to write this () or super ()?" I am more stupid, and thought for a long time, the Internet to check the information, read the book only understand.

Before you say this, you have to understand the meaning of super () and this ():

    1. This () exists, I personally think the reason is very simple, is for the reuse of code (purely personal point of view, do not know whether it is correct)
    2. The reason why super () exists is very important! Because the child class is an inheritance relationship from the parent class, inheritance means that all the member variables and methods in the parent class are obtained. Since these members of the parent class are available, it means that the subclass can access the methods and member variables of the parent class (which, of course, is not meant to be used in any way, using the premise that it must have the appropriate access rights), and if the child class is using the parent class member, the member variables in the parent class are not properly initialized. Then there will be unexpected errors, so the problem is here, how to ensure that the parent class members are safe? This ensures that the parent class is properly initialized, and that the proper initialization of the parent object is inseparable from the constructor of the parent class, and the meaning of the super existence is exactly the same. In the subclass's constructor, if Super () is not explicitly used, and the first row does not call the constructor of the subclass with this (), the compiler will automatically call the default constructor of the parent class with super () in the first line, if the parent does not have a default constructor.

Why do you have to be in the first line?

The reason for super () in the first line is that the subclass may have access to the parent class object, such as using the parent class object's member functions and variables in the constructor, using the parent class in the member initialization, using the parent class in the code block, etc. to ensure that the parent class object is initialized before the child class can access the parent class object

The reason for this () in the first line is: to guarantee the uniqueness of the initialization of the parent class object. We assume that Class B is a subclass of Class A, and what happens if this () can be used on any row of the constructor? First the program runs to the first line of constructor B (), finds that no call to this () and super (), automatically completes super () on the first line, finishes initializing the parent class object, and then returns the constructor of the subclass to continue execution when running to the "this (2)" of constructor B (); , the B (int) constructor of the Class B object is called, and the parent class object is initialized again in B (int)! This creates a waste of resources, of course, it can also cause some unexpected results, anyway, it is unreasonable, so this () can not be present in addition to the first row of other rows!

When you create a subclass object, the object contains a child object of the parent class, whether or not you use it.

Thinking in Java note, if there is a wrong place, also look at ^_^

Why does the constructor for a Java constructor only write this () or super () in the first line?

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.