Java Inheritance Initialization

Source: Internet
Author: User
Tags constructor inheritance

It is necessary to have an understanding of the entire initialization process, including inheritance, which has a holistic concept of what is happening in the process. Please observe the following code:

: Beetle.java
//The full process of initialization.

Class Insect {
  int i = 9;
  Int J;
  Insect () {
    prt ("i =" + i + ", j =" + j);
    j = k;
  }
  static int x1 = 
    prt ("Static insect.x1 initialized");
  static int prt (String s) {
    System.out.println (s);
    return;
  }

public class Beetle extends insect {
  int k = PRT ("beetle.k initialized");
  Beetle () {
    prt ("k =" + K);
    PRT ("j =" + j);
  }
  static int x2 =
    prt ("Static beetle.x2 initialized");
  static int prt (String s) {
    System.out.println (s);
    return to;
  }
  public static void Main (string[] args) {
    prt ("Beetle constructor");
    Beetle B = New Beetle ();
  }
///:~


The output of the program is as follows:

Static insect.x initialized
static beetle.x initialized
beetle constructor
i = 9, j = 0
beetle.k Initiali Zed
k = 63

When running Java on Beetle, the first thing that happens is that the loader finds that class outside. During the loading process, the loader notices that it has a base class (that is, the meaning to be expressed by the extends keyword), so it is loaded with it. This process occurs regardless of whether you are ready to generate an object for that underlying class (try to annotate the object's creation code as a comment and verify it yourself).
If the underlying class contains another underlying class, then another base class is loaded, and so on. Next, static initialization is performed in the root base class (now insect), then in the next derived class, and so on. Ensuring this order is critical, because the initialization of the derived class may depend on the proper initialization of the underlying class members.
At this point, the necessary classes are all loaded, so the object can be created. First, all of the base data types in this object are set to their default values, and the object handle is set to NULL. The underlying class builder is then invoked. In this case, the call is made automatically. But it is also perfectly possible to use super from a row to specify a builder call (as in the first operation in the Beetle () builder). The base class is built in exactly the same process as the derived class builder. After the foundation is finished, the instance variables are initialized in their original order. Finally, the main body part of the builder remains executed.

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.