Java entry note (3): initialization sequence, java Initialization

Source: Internet
Author: User

Java entry note (3): initialization sequence, java Initialization

Initialization order rules

1. When a class object is instantiated, the member variables are first initialized before the constructor is called, regardless of the writing order. If no Explicit initialization is performed before the constructor is called, the default value is assigned.

The reason for this is that the initial values of some member variables may be used during the constructor's execution.

2. static variables are initialized before all other class member variables, regardless of the writing order. However, static variables are initialized only once when the class is used for the first time.

3. The base class constructor is always called during the construction of the exported class, and is gradually linked up according to the inheritance level (the call order starts from the base class ). It can be understood that the logical relationship in this way is that the Members and methods of the parent class may be used when a class is built. The order is the opposite when cleaning.

4. The member initialization method (including basic data type assignment) will be called only after the base class constructor calls it. Initially, the bucket allocated to the object initializes the binary zero.

For example, from section 5.7.2 of Java programming ideology, the initialization sequence is reduced and re-numbered to facilitate demonstration. Use the parameters of the constructor to indicate the execution sequence. Example 1 ~ Two rules:

class Bowl {    Bowl(int marker) {        System.out.println("Bowl(" + marker + ")");    }}class Cupboard {    Bowl bowl1 = new Bowl(3);    static Bowl bowl2 = new Bowl(1);    int i;    static int j = 5;    Cupboard() {        System.out.println("i:" + i);        bowl4 = new Bowl(j);        j = 6;     }    Bowl bowl3 = new Bowl(4);    static Bowl bowl4 = new Bowl(2);}public class ParaInitialization {    public static void main(String args[]) {        new Cupboard();        new Cupboard();    }}

Output and corresponding Annotations:

Bowl (1) // The first static variable
Bowl (2) // The second static variable
Bowl (3) // the first non-static member variable of the first object
Bowl (4) // the first non-static member variable of the first object
I: 0 // initialization member variables are not displayed
Bowl (5) // change the value of the static variable
Bowl (3) // the first non-static member variable of the second object
Bowl (4) // The second non-static member variable of the second object
I: 0
Bowl (6)

Example 2 shows a simple example of 3rd rules.

class A {    A() {        System.out.println("A");    }}class B extends A {    B() {        System.out.println("B");    }}class C extends B {    C() {        System.out.println("C");    }}public class hrt {    public static void main(String args[]) {        new C();    }}

Output

A
B
C

Example 3 is used to demonstrate Rule 4. When the parent class constructor is called, the method in the constructor overwrites the method quilt class method.

class Glyph {    void draw() {        System.out.println("Glyph.draw(");    }    Glyph() {        System.out.println("Glyph() before draw()");        draw();        System.out.println("Glyph() after draw()");    }}class RoundGlyph extends Glyph {    int radius = 1;    RoundGlyph(int r) {        radius = r;        System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);    }    void draw() {        System.out.println("RoundGlyph.draw(), radius = " + radius);    }}public class PolyConstructors {    public static void main(String[] args) {        new RoundGlyph(5);    }}

So many rules are really impressive. Orchestrating them in order will be much easier to read.

Object initialization order. If there is a corresponding member/parent class, the corresponding entry is executed:

1. initialize the bucket allocated to the object to a binary zero;

2. Call the base class constructor, starting from the top/root base class;

3. In the declared order, use direct values or initialization methods. initialize static variables in sequence, and then initialize non-static variables in sequence;

4. Call the constructor of the class to which the object belongs.

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.