Ix. sequence of initialization of Java programs (ii)

Source: Internet
Author: User
In one of the previous blogs I wrote about the sequence of initialization of programs in a class, but in Java object-oriented, there are still inheritance relationships between classes. So about the initialization order of the program, we can be subdivided into: The parent class static variable, the parent class static code block, the parent class constructor, the parent class non-static variable, the parent non-static code block, the subclass static variable, the subclass static code block, the subclass constructor, the subclass non-static member variable and the subclass non-static code block
This blog we are talking about the process of initializing the program, the above members in the initialization of the order of loading.

In the previous discussion we concluded that in a class, the order in which Java programs are loaded is: static variables----Static code blocks---Non-static variables--non-static code block---constructors.

The code for the parent class:
 Public classSuperclass {//both the parent class and the subclass are in a package, where we use the default modifier//This is a static variable of a parent class, which is also the default value for initialization null    StaticString Superstaticvariale; //static code block, assigning a value to string    Static{Superstaticvariale= "Parent class static code block assignment succeeded"; System.out.println ("The static code block for the parent class is now running:" +Superstaticvariale); }    //non-parametric construction, overriding the value of a static code blockSuperclass () {Superstaticvariale= "Parent class constructor assignment succeeded"; System.out.println ("The constructor for the parent class is now running:" +Superstaticvariale); }    //define a non-static variableString Supervariale; //define a non-static block of code{Supervariale= "Parent class non-static code block assignment"; System.out.println ("This is the non-static code block for the parent class:" +Supervariale); }}
The code for the Child class:
 Public classSubclassextendssuperclass{StaticString Substaticvariale; //static code block, assigning a value to string    Static{Substaticvariale= "Sub-class static code block assignment succeeded"; System.out.println ("This is the static code block for the subclass:" +Substaticvariale); }    //non-parametric construction, overriding the value of a static code blockSubclass () {Superstaticvariale= "Sub-class constructor assignment succeeded"; System.out.println ("The constructor for the subclass is now running:" +Superstaticvariale); }    //define a non-static variableString Subvariale; //define a non-static block of code{Subvariale= "Sub-class non-static code block assignment"; System.out.println ("This is a subclass of non-static code block:" +Subvariale); }}
Test code:
 Public class Main {    publicstaticvoid  main (string[] args) {          New  Subclass ();    }}
 Run Result: 
'
is running a static code block for the parent class: The parent static code block assignment succeeds
at this point, the static code block for the subclass is running: Subclass static code block assignment succeeded
The non-static code block for the parent class is now running: the parent class non-static code block assignment value
The constructor for the parent class is now running: the parent constructor assignment succeeds
at this point, the subclass non-static code block: Subclass non-static code block assignment
is the constructor of the subclass: Subclass constructor Assignment succeeded
"'
Obviously, in an inheritance relationship, The order in which the code is loaded is the static variable of the parent class----The static code block of the parent class--Class static variable--the static code block of the subclass--The parent class non-static variable---the parent class's non-static code block--The parent class's constructor-- Subclass non-static variable--subclass non-static code block--child class constructor

Further testing:
 public  class   Main { public  static  void   main (string[] args) {subclass s  = new  
  
    subclass ();        Subclass S1  = 
   new  
    subclass ();    Subclass S2  = 
   new  
    subclass (); }}
  
Operation Result:
```
The static code block for the parent class is now running: The parent class static code block assignment succeeds
The static code block for the subclass is now running: Subclass static code block assignment succeeded
The non-static code block for the parent class is now running: Parent non-static code block assignment
The constructor for the parent class is now running: the parent constructor assignment succeeded
The subclass non-static code block is now running: Subclass non-static code block assignment
The constructor for the subclass is now running: the subclass constructor Assignment succeeded
The non-static code block for the parent class is now running: Parent non-static code block assignment
The constructor for the parent class is now running: the parent constructor assignment succeeded
The subclass non-static code block is now running: Subclass non-static code block assignment
The constructor for the subclass is now running: the subclass constructor Assignment succeeded
The non-static code block for the parent class is now running: Parent non-static code block assignment
The constructor for the parent class is now running: the parent constructor assignment succeeded
The subclass non-static code block is now running: Subclass non-static code block assignment
The constructor for the subclass is now running: the subclass constructor Assignment succeeded
```
concluded that:
The static code of the parent class and the child class is executed only once, and then the non-static code block is combined with the constructor.

Simplify the code:
 Public classMain { Public Static voidMain (string[] args) {c C=NewC (); }}classa{A () {System.out.println ("A's parameterless constructor"); }}classBextendsa{//B (int a) {B () {System.out.println ("B's parameterless constructor"); }}classCextendsb{C () {System.out.println ("C's parameterless constructor"); }}
Operation Result:
"' Text
A non-parametric constructor
Non-parametric constructors for b
Non-parametric constructors for C
```
The constructor that calls C generates an instance object of C that starts with the parameterless constructor of the parent class of the ancestor, and then our class inherits a super-parent class object, which is where we call student's parameterless construct in our original error code to create an object. First, the parameterless constructor of the object's parent class object is called.
class student{   String name;      {      = "eldest brother";   }      Student () {       this (name);   This will error      Super();      System.out.println ("topic requires writing a parameterless constructor");      Student (String name) {      this. Name= name;      SYSTEM.OUT.PRINTLN (name);   }   }
The subclass instantiates a default call to the parent class of the parameterless constructor, i.e., before the this call is in Super () (the two do not appear at the same time), the name is not a static property at this time, error: cannot reference name before calling the superclass constructor.
class student{   static  String name;      {      = "eldest brother";   }      Student () {       this(name);      System.out.println ("topic requires writing a parameterless constructor");      Student (String name) {      this. Name= name;      SYSTEM.OUT.PRINTLN (name);   }   }
When name is a static property, the code block is non-static when compiled through, calling the subclass of the parameterless constructor when this (name), the output is:
"' Text
Null
The topic asks for an argument-free constructor.
```
The This () call to the argument construct at this time is not assigned successfully.
 class   student{ static   String name;  static  {name  = "Boss" ;      } Student () { this   (name);   System.out.println ( "title requires writing an parameterless constructor"  this . Name = name      ;   SYSTEM.OUT.PRINTLN (name); }}
 run result at this time: 
"text
Boss
Topic required to write a parameterless constructor
" "
so that the assignment succeeds. This proves that our conclusion is correct, this () is the Operation Super () before the subclass parent class constructor, and when the subclass code block is non-static, the subclass non-static code block executes after the parent class constructor is executed, so this name is not assigned a value. So printing is null.

Conclusion:
1. A class can call a parameter constructor of this class in the parameterless constructor (in reverse order);
2. When executing a subclass of the parameterless constructor, the highest-level parent class is called by default without a parameter construct, and is called up to the non-parametric construction of the subclass;
3. The Java program loads as a static variable of the parent class--The static code block of the parent class--The static code block of the child class----and the parent class non-static variable---the parent class's non-static code block--The parent class's constructor-- Subclass non-static variable--subclass non-static code block--child class constructor, and static variable or code block no matter how many times the constructor is called, he executes only once, and then calls the constructor to execute the non-static property and code block constructor.

Finally about why the subclass calls the constructor of the parent class, which is designed to initialize a property inherited from the parent class, and the subclass needs to know how the parent class initializes the property.
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.