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