Java Development Handbook The 10th chapter of the learning process constructor application

Source: Internet
Author: User

Constructor and return type:

The constructor does not have a return type, and if you add the return value type (including void) to the constructor, the compilation does not error, but it is no longer a constructor, but just a method with the same name as the class in which it resides.

Constructors and methods are two different concepts:

  • A constructor cannot be called by an object reference as a method.
  • The constructor is the code to be executed when the object is created, called by New. A method is a class or object that has the behavior that is called by a reference.

parameterless Constructor (Source: Java Core Technology Vol. 1 (Chinese 9th edition), 127 pages):

If you do not write a constructor when you write a class, the system provides a parameterless constructor. This constructor sets all instance fields to their default values (if the instance domain is not initialized in the class).

If there is at least one constructor provided in the class, but no parameterless constructor is provided, it is considered illegal to construct the object without supplying the parameter.

Questions about constructors and inheritance relationships (cascade calls to constructors):

classanimal{ PublicAnimal () {System.out.println ("I am animal's constructor!" "); }}classBirdextendsanimal{ PublicBird () {System.out.println ("I am Bird's constructor!" "); }} Public classsample10_6{ Public Static voidMain (string[] args) {NewBird (); }}
    • Executes the new Bird () statement and enters the Bird constructor body.
    • Call the animal constructor and enter the animal constructor body, because animal is the parent of bird.
    • Call the constructor of object to enter the object constructor body, because object is the parent class of animal.
    • Executes the code in the object constructor until it is complete, returning the animal constructor.
    • Executes the code in the animal constructor until it is complete, returning the bird constructor.
    • Executes the code in the bird constructor until it is complete.

If the specified parent class or sibling constructor is not explicitly invoked in the constructor, the compiler automatically adds the code that calls the parent class parameterless constructor as the first sentence of the constructor code.

From the above, because of cascading calls, the code that calls the parent class constructor itself must be in the first sentence of the constructor, or the compilation will be an error. That is, in the first sentence to write super and then add a pair of parentheses, the parentheses placed in the corresponding parent class constructor required parameters. So, if developers need to write their own, the compiler will no longer automatically add code that calls the parent class (parameterless) constructor.

In real development, if you do not write code calling the parent class constructor in the constructor, the system can only automatically invoke the parameterless constructor of the parent class, and if you need to explicitly call a constructor in the parent class, you must write your own code.

The reason the constructor was designed for cascading calls:

Depending on the principle of substitution, the subclass object must be able to override the parent class object, so before constructing the subclass object, first construct a parent class object and then further construct the subclass object on the basis of the parent class object. The subclass object in Java is implicitly a parent class object, which is further crafted on the basis of the parent class object.

Call the sibling constructor:

Once the "This (XX)" Call to the sibling constructor is used, the compiler will no longer automatically add "super ()", in which case the parameterless constructor of the parent class will no longer be automatically called by the system in the constructor. Because this (XX) executes to the called constructor, the "super ()" that the compiler automatically adds to it in the constructor is executed. Or a super statement written by the developer himself.

Single-column mode:

classsingleton{Private StaticSingleton singleinstance;  Public StaticSingleton getinstance () {if(SingleInstance = =NULL) {singleinstance=NewSingleton (); }        returnsingleinstance; }    PrivateSingleton () {System.out.println ("The constructor that executes a single-Column pattern Class!! "); }} Public classsample10_15{ Public Static voidMain (string[] args) {Singleton S1=singleton.getinstance (); Singleton S2=singleton.getinstance (); if(S1 = =S2) {System.out.println ("Two references point to the same object! "); }        Else{System.out.println ("Two references point to a different object!" "); }}} The result of the output is: the constructor that executes a single-Column pattern Class!! Two references point to the same object! 

Although a two-time object reference was obtained, the constructor was executed only once, stating that only one object was created.

Two times gets the reference equal and also indicates that there is only one object.

Loading of the program:

in Java, the total load process when an object is created is as follows :

  • First load the class to create the object and its immediate parent class with the indirect parent class.
  • Static members are loaded while the class is loaded, including initialization of static member variables, execution of static statement blocks, and execution in code order at load time.
  • After the required class loading is complete, the object is created. Non-static members are loaded first, including initialization of non-static member variables, execution of non-static member blocks, and execution in code order at load time.
  • Finally executes the constructor. When the constructor finishes executing, the object is generated.

The above paragraph is a summary of the Java Development Handbook book.

And my summary is:

  • First load the class to create the object and its immediate parent class with the indirect parent class.
  • In this step of loading the class, the top-level class of the object class is loaded, and then loaded by the top-level class to the next layer of class, ... until the class of the object is loaded.
  • In the previous step, when loading each class, the static members are loaded first, as is the summary in the book.
  • After the required class loading is complete, the object is created.
  • In this step of creating an object, you first create an object of the top level class, and then you create the object of the next layer class ... until all the objects have been created.
  • In the previous step, when you created each object, the non-static members were loaded, and then the constructors of that class were executed before the object was created.

Java Development Handbook The 10th chapter of the learning process constructor application

Related Article

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.