About the initialization order of Java

Source: Internet
Author: User
Tags class definition

1. Java initialization order without inheritance:

Class Sample
{
Sample (String s)
{
System.out.println (s);
}
Sample ()
{
SYSTEM.OUT.PRINTLN ("sample default constructor called");
}
Main function
public static void Main (String str[])
{
Test a=new test ();
}
}
Class test{
Static sample Sam=new sample ("Static member Sam initialization");
Sample Sam1=new sample ("SAM1 member initialization");
static{
System.out.println ("Static Block Execution");
if (sam==null) System.out.println ("Sam is null");
Sam=new Sample ("Initialize SAM member variable in static block");
}
Test ()
{
SYSTEM.OUT.PRINTLN ("test default constructor called");
}
}


The output is as follows: Static member Sam initialization-----Static member initialization statically block execution-----Static block is executed in static block initialization SAM member variable----static block performs SAM1 member initialization-----Normal member initialization Tes The T default constructor is called-----constructor execution

It can be concluded that a static member variable is initialized first (note that static can be viewed as a static member, whose execution order is related to the order in which it is declared in the Class) B normal member Initializes c to execute the constructor.
For a static member (a static block can be considered as a normal one, it does not necessarily execute first when class is initialized) and ordinary members, whose initialization order is only relevant to the order in the class definition, regardless of other factors. For example, the following example:

classtest{Static{System.out.println ("Static block 1 execution"); }StaticSample staticsam1=NewSample ("Static member staticSam1 initialization"); Sample sam1=NewSample ("SAM1 member initialization");StaticSample staticsam2=NewSample ("Static member StaticSam2 initialization");Static{System.out.println ("Static block 2 Execution");       Test () {System.out.println ("Test default constructor called"); } Sample sam2=NewSample ("SAM2 member initialization");
}

The result is: Static Block 1 Performs a staticSam1 initialization of a static member StaticSam2 initialize the SAM1 static block 2 executes the--------statically member initialization sam2 Member initialization--------normal member test default constructor is called--------constructor

2. Initialization order in Java inheritance:

Class test{
static{
System.out.println ("Parent class static block 1 execution");
}
Static sample Staticsam1=new sample ("Parent static member STATICSAM1 initialization");
Sample Sam1=new Sample ("Parent class SAM1 member initialization");
Static sample Staticsam2=new sample ("Parent static member StaticSam2 initialization");
static{
System.out.println ("Parent class static block 2 Execution");
}
Test ()
{
System.out.println ("Parent class test default constructor called");
}
Sample Sam2=new Sample ("Parent class SAM2 member initialization");


public static void Main (String str[]) {
TestSub a=new testsub ();
}
}




Class TestSub extends Test
{
Static sample Staticsamsub=new sample ("Subclass static member Staticsamsub initialization");
TestSub ()
{
System.out.println ("subclass TestSub default constructor called");
}
Sample Sam1=new sample ("Subclass SAM1 member initialization");
Static sample Staticsamsub1=new sample ("Subclass static member StaticSamSub1 initialization");

Static{system.out.println ("Subclass static Block Execution");
Sample Sam2=new sample ("Subclass SAM2 member initialization");

}

Execution Result: parent class static Block 1 staticSam1 initialization parent static member StaticSam2 Initialize parent Class A static block 2 execute--------parent class statically member Initialize subclass static member Staticsamsub initialize subclass static member StaticSamSub1 initialization subclass static block execute-------Subclass statically member Initialize parent class SAM1 member Initialize Parent Class SAM2 member initialization parent class test default constructor is called-------Parent class normal member initialization and constructor execution subclass SAM1 member initialization Subclass SAM2 member initialization Subclass TestSub Recognition constructors are called-------Normal member initialization and constructor execution of the parent class

This concludes the Java initialization Order conclusion: 1 initialization of all static members of the inheritance system (late Father class, sub Class) 2 The parent class initialization completes (the normal member initializes the--> constructor call) 3 subclass initialization (normal member--> 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.