Getting Started with Java (iii): initialization order

Source: Internet
Author: User

Rules for Initialization order

1. When an object of a class is instantiated, the member variable is initialized first before the constructor is called, regardless of the order of writing. If there is no explicit initialization before the constructor is called, the default value is assigned.

The reason for this is that the constructor execution may use the initial value of some member variables.

2.static variables are initialized earlier than all other class member variables, as well, regardless of the writing order. However, the static variable is initialized only once for the first time that the class is used.

3. The base class constructor is always called during the construction of the exported class, and is incrementally linked at the inheritance level (the invocation order starts from the base class). It can be understood that the logical relationship is that when a class is built, the members and methods of its parent class may be used. The order is reversed during cleanup.

4. The initialization method of the member, including the assignment of the base data type, is not invoked until the base class constructor is called. Initially, the storage space allocated to the object initializes the binary 0.

Example one from the Java programming thought 5th 7.2, in order to facilitate the presentation of the initialization sequence, has been reduced and renumbered. Use the constructor's parameters to indicate the order of execution and to demonstrate the 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 Cupboar D (); }}

Output and corresponding annotations:

Bowl (1)//First static variable
Bowl (2)//second static variable
Bowl (3)//first non-static member variable of first object
Bowl (4)//first non-static member variable of first object
i:0//not displaying initialized member variables
Bowl (5)//Change the value of a static variable
Bowl (3)//The first non-static member variable of the second object
Bowl (4)//second non-static member variable of second object
i:0
Bowl (6)

Example two is a simple example that demonstrates the 3rd rule.

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 three is used to demonstrate rule 4. When the parent class constructor is called, the method in the constructor is overridden by the 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);}}  

With so many rules, it really makes a big head of mind. It is easier to read them sequentially.

Object initialization order, if there is a corresponding Member/parent class to execute the corresponding entry:

1. Initialize the storage space allocated to the object to binary 0;

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

3. In the order of declaration, using the direct assignment or initialization method, initialize the static variable in sequence, then initialize the non-static variable sequentially;

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

Getting Started with Java (iii): initialization order

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.