1. Initialize the storage space allocated to the object to 0 before any event occurs;
2, starting from the base class recursion, call static code block, static member initialization method according to the order of declaration in class.
But there are two exceptions:
- Member variables defined as final non-static basic data types are also initialized at this time;
- There are only string member variables defined as final non-static, and the "=" assignment initialization is performed (not new). We know there's No in Java
The concept of operator overloading, a class that can be directly assigned by "=" has only a final class that is implemented inside Java such as String or integer. Oddly enough, only string is initialized,
Integer and so on, only to see the implementation of the Java Virtual machine
3, starting from the base class, call the non-static code block and non-static member (except for several final special cases in 2) initialization method according to the order of declaration in the class, then call the constructor, then recursively initialize the subclass;
Note: calling a function that has polymorphic behavior inside a parent class constructor will result in unpredictable results, because the subclass override method is called at this time, and all subclasses of the class
non-static members may not have been initialized and will not get the results we want.
Test Platform: win10 (+) + jre1.8.0_73 + Eclipse
Test code:
Base class
public class Baseinit {
static {
System.out.println ("<<< base class static code block >>>");
}
Public Baseinit () {
System.out.println ("<<< base class constructors >>>");
Call the override function of a subclass to access a non-static member variable that is not initialized by the child class
Dangertest ();
}
{
System.out.println ("<<< base class non-static code block >>>");
}
Need override
public void Dangertest () {}
}
Sub-class:
public class Deriveinit extends Baseinit {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Deriveinit ();
}
/**
* Constructor function
*/
Public Deriveinit () {
System.out.println ("<<< subclass Constructors >>>");
if (testnonstatic = = 555)
{
System.out.println (" Member variable has been initialized ");
}
}
/**
* static
*/
static private int teststatic =;
Static {
System.out.println ("<<< Subclass Static code block >>> ");
if (teststatic = = 100 )
{
System.out.println ("Initialize in the same class statically in order of declaration");
}
}
/**
non-static
* *
private int testnonstatic = 555;
{
System.out.println ("<<< Sub-class non-static code block >>> ");
if (testnonstatic = = 555)
{
System.out.println ("Non-static in the same class initialized in order of declaration");
}
/**
* Accessing a non-static member variable of a subclass in a base class
*/
private int derive0 = 888;
Final private int derive1 = 888;
Final private Integer derive2 = 888;
Final private String derive3 = new String ("Hello World");
Final private String derive4 = "Hello World";
@Override
public void Dangertest ()
{
System.out.println ("subclass member Variable DERIVE0 =" + derive0);
System.out.println ("subclass member Variable derive1 =" + derive1);
System.out.println ("subclass member Variable derive2 =" + derive2);
System.out.println ("subclass member Variable Derive3 =" + Derive3);
System.out.println ("subclass member Variable Derive4 =" + derive4);
}
}
Operation Result:
<<< base class static code block >>>
<<< Subclass Static code block >>>
Static code in the same class is initialized in the order of Declaration.
<<< base class non-static code block >>>
<<< base class Constructors >>>
Child class member Variable DERIVE0 = 0
Child class member Variable DERIVE1 = 888
Child class member Variable derive2 = NULL
Child class member Variable DERIVE3 = NULL
Child class member Variable DERIVE4 = Hello World
<<< Subclass non-static code blocks >>>
Non-static code in the same class is initialized in the order of declaration
<<< sub-class constructors >>>
Member variable already initialized
--for reference only, welcome to exchange--
Java Initialization Order