Initialization of the Java class

Source: Internet
Author: User

Java provides two different types of initialization, namely initialization of classes and initialization of objects. Class members are static, and a value is set by default. The initialization of the object is done inside the constructor. However, if you want to assign a static variable to a non-default value, or initialize a class of common object properties (regardless of which constructor is called), you need some special methods. Static initialization blocks and non-static initialization blocks are provided to handle both cases.

    1. Static initialization blocks

Static initialization blocks are defined by static{} . A simple code example is as follows:

public class Corderstatic {staticint idx;static{    System.out.println ("idx:" + integer.tostring (idx++));    System.out.println ("idx:" + integer.tostring (idx));} publicstatic void Main (string[] argv) {    System.out.println ("Done");}}
The result of the code run is:

idx:0

Idx:1

Done

From the output you can see that the static initialization block is called after the initialization of the static member. And if more than one static initialization block exists in the class, it is called according to the order in which it occurs.

    1. Non-static initialization block

Non-static initialization blocks are defined in the {} block. Its difference from static initialization blocks is that there is no static keyword rhetoric. The test code is as follows:

public class Corderinstance {    int idx;{    System.out.println ("idx:" + integer.tostring (idx++));    System.out.println ("idx:" + integer.tostring (idx++));} Publiccorderinstance () {    System.out.println ("Idxin constructor:" + integer.tostring (idx));} public static void Main (string[] argv) {    newcorderinstance ();    System.out.println ("Done");}}

The code output is as follows:

idx:0

Idx:1

Idxin Constructor:2

Done

Visible, non-static initialization blocks are called before the constructor call ends when the object property is initialized.

    1. Order of initialization

If a class defines both static and non-static initialization blocks, which one will be executed first? The test code is as follows:

public class Corder {static int staticpro;static{    System.out.println ("Staticblock:" + integer.tostring (Staticpro) );} int Instancepro; {    System.out.println ("non-staticblock." + integer.tostring (instancepro++));} Publiccorder () {    System.out.println ("Instanceproperty:" + integer.tostring (instancepro++));    System.out.println ("Theconstructor had been complete."); publicstatic void Main (string[] argv) {    newcorder ();}}


The code output is as follows:

staticblock:0

non-staticblock.0

Instanceproperty:1

Theconstructor had been complete.

As can be seen, static initialization blocks are called first, then non-static initialization blocks, and finally constructors.

    1. Inheritance and initialization

The initialization block becomes more complex when it encounters class inheritance. First put the test code out:

public class Corderinherit {static int staticpro;statica a;static{System.out.println ("staticinitialization:" + integ    Er.tostring (Staticpro));    if (a = = null) {System.out.println ("ClassA is null."); } a= new B (10);} Intinstancepro; A AA;    {instancepro= 10;    System.out.println ("Specialblock." + integer.tostring (Instancepro)); Aa= New B (10);}    Publiccorderinherit (int i) {System.out.println ("Instanceproperty:" + integer.tostring (Instancepro));    instancepro= i;    System.out.println ("Staticproperty:" + integer.tostring (Staticpro));    System.out.println ("Instanceproperty:" + integer.tostring (Instancepro));    System.out.println ("Theconstructor had been complete.");    publicstatic void Main (string[] argv) {Newcorderinherit (-1);    }}class A {static int index;    static{System.out.println ("ClassA static 1" + integer.tostring (index++));    } {System.out.println ("ClassA Special 1" + integer.tostring (index++)); } publicA () {System.out.println ("Constructclass A." + integer.tostring (index++));    } static{System.out.println ("ClassA static 2" + integer.tostring (index++));    } {System.out.println ("ClassA Special 2" + integer.tostring (index++));     }}class B extends A {static int index;     static{System.out.println ("ClassB static 1" + integer.tostring (index++));     } {System.out.println ("ClassB Special 1" + integer.tostring (index++));     } public B (int i) {System.out.println ("Constructclass B." + integer.tostring (index++));     } static{System.out.println ("ClassB static 2" + integer.tostring (index++));      } {System.out.println ("ClassB Special 2" + integer.tostring (index++)); }}


The code output is as follows:

staticinitialization:0

ClassA is null.

ClassA Static 1 0

ClassA Static 2 1

ClassB Static 1 0

ClassB Static 2 1

ClassA Special 1 2

ClassA Special 2 3

Constructclass A.4

ClassB Special 1 2

ClassB Special 2 3

Constructclass B.4

specialblock.10

ClassA Special 1 5

ClassA Special 2 6

Constructclass a.7

ClassB Special 1 5

ClassB Special 2 6

Constructclass b.7

Instanceproperty:10

staticproperty:0

Instanceproperty:-1

Theconstructor had been complete.

Observing the above output, you can see that when instantiatingBObject, the virtual opportunity is loadedB.class, but becauseBInheritedASoA.classwill also be loaded. This leadsAAndBThe static initialization blocks are called separately. StructureBThe object,ANon-static initialization blocks and default constructors are called, and then theBNon-static initialization blocks and constructors.

    1. Conclusion

As can be seen from the above example, the complete initialization order of a class is as follows (the subclass is the current class, or the class to be new ):

    1. Static initialization blocks of the parent class are called in order of occurrence

    2. Assigning a default value to a static member of a subclass

    3. static initialization block calling subclasses in order of occurrence

    4. Assign to parent class object property default

    5. Call the non-static initialization block of the parent class object in order

    6. Call the parent class constructor

    7. Assign Subclass object property default value

    8. Call Subclass constructor

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.