The execution order of Java parameter-free constructors

Source: Internet
Author: User

Here's a piece of code that uses an array construct stack to illustrate

1  Public classStackarray<e> {2 3     Privateobject[] data =NULL;4     Private intMaxSize;//Stack Capacity5     Private inttop =-1;//stack Top pointer6     7     /**8 * Override parameterless constructor9      */Ten Stackarray () { One          This(10);//The default stack size is 10 (if there is code in front of this line, this line will give an error) ASYSTEM.OUT.PRINTLN ("No parameter constructor execution"); -     } -      the     /** - * Parametric Constructors -      */ -Stackarray (intinitialsize) { +         if(initialsize >= 0){ -              This. maxSize =initialsize; +data =NewObject[initialsize]; Atop =-1; at}Else{ -             Throw NewRuntimeException ("Initialization size cannot be less than 0:" +initialsize); -         } -SYSTEM.OUT.PRINTLN ("parameter constructor execution")); -     } -      in      Public Static voidMain (string[] args) { -         NewStackarray (); to         //New Stackarray (a); +     } -}

1) New Stackarray () is an instantiation process without parameters:

Find the parameterless constructor > initialize the member variable (the reference type is initialized to null,int initialized to 0) > this (10) calls the parameter constructor > comes to the parameter constructor and initializes the assignment parameter (initialsize=10) > Member variable assignment (for example, top=-1) > Execute parameter Constructor internal code (output "parameter constructor execution") > Execute parameterless constructor internal code (output "No parameter constructor execution") > End

2) New Stackarray (20) has a parameter instantiation process:

Locate the parameter constructor > initialize the assignment parameter and initialize the member variable (the reference type is initialized to null,int initialized to 0) > member variable assignment (for example, Top=-1) > Execute code inside the Argument builder > End

3) If no parameter constructor is called within the parameterless constructor, the instantiation process of New Stackarray ():

Find parameterless constructor > Initialize member variable (reference type initialized to null,int initialized to 0) > member variable assignment (e.g. Top=-1) > execute parameterless constructor internal code (output "parameterless constructor execution") > End

The execution order of Java parameter-free constructors

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.