Home » Java » Java class instantiation order

Java class instantiation order

public, class, Initializing {
Public, static, void, main (String[], args) {
New, TestSub ();
}
}
Class Sample {
Sample (String, s) {
System.out.println (s);
}
Sample () {
System.out.println (the Sample default constructor is called);
}
}
Class Test {
Static {
System.out.println (parent class, static block, 1 execution);
}
Static, Sample, staticSam1 = new, Sample ("parent class, static member, staticSam1 initialization");
{
System.out.println ("parent class non static 1 block execution");
}
"Sample sam1 = new Sample" ("parent class sam1 member initialization");
Static, Sample, staticSam2 = new, Sample ("parent class, static member, staticSam2 initialization");
Static {
System.out.println (parent class, static block, 2 execution);
}
Test () {
System.out.println (the parent class Test default constructor is called);
}
"Sample Sam2 = new Sample" ("parent class Sam2 member initialization");
{
System.out.println ("parent class non static 2 block execution");
}
}
Class, TestSub, extends, Test {
Static, Sample, staticSamSub = new, Sample (subclass static member, staticSamSub initialization);
TestSub () {
System.out.println ("subclass TestSub default constructor is called");
}
{
System.out.println (subclass, non static, 1 block execution);
}
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);
}
{
System.out.println (subclass, non static, 2 block execution);
}
Sample Sam2 = new Sample ("subclass Sam2 member initialization");
}


The parent class static block 1 executes

The parent class static member staticSam1 initializes

The parent class static member staticSam2 initializes

The parent class static block 2 executes

Subclass static member staticSamSub initialization

Subclass static member staticSamSub1 initialization

Subclass static block executes

The parent class performs
not static 1 blocks
The parent sam1 member initializes the

The parent Sam2 member initializes the

The parent class performs
not static 2 blocks
The parent class Test default constructor is called

Subclasses of non static 1 blocks perform

Subclass sam1 member initialization

Subclasses of non static 2 blocks perform

Subclass Sam2 member initialization

The subclass TestSub default constructor is called


The

1. static member is the same priority as the static block of code, which determines the initialization order


2. non static members are the same priority as non static code blocks, and the sequence determines the initialization order,

3. the parent class, all static keywords are always the first to execute, and all static of the parent class executes after the execution subclass of all static, including the member variables and the code block

The 4. subclass constructor has the lowest priority, and finally executes the

The 5. parent constructor is the lowest in the parent class, but it is prior to all subclasses, not the static section

Latest