initialization order
See also static
initialization.
■ The general order of class initialization is:
1 - First all static items are initialized, in their source code order..
2 - Then all member variables are initialized.
3 - Lastly the constructor is called.
■ Below is a simple demonstration program showing class initialization order. Its output is:
1a
- Static class variable vstatic initialized first.
1b
- Static initialization block ran.
2 - Non-static class variable vnonstatic
initialized.
3 - Constructor ran.
public
class ExecuteDemo {
public static void main(String[ ] args)
{
DemonstrateOrder DO =
new DemonstrateOrder ( ); }
}
class
DemonstrateOrder{
static String vstatic = verify(" 1a
- Static class variable vstatic initialized first." );
String vnonstatic = verify(" 2 - Non-static class variable vnonstatic
initialized." );
static {System.out.println(" 1b -
Static initialization block ran." ); }
static String verify(String s) {
System.out.println(s); return s; }
DemonstrateOrder ( )
{System.out.println(" 3 -
Constructor ran." ); }
}
■ The general order of initialization for classes with inheritance is:
First, parent static items, then child static items
Then parent member variables, followed by the parent constructor
Then child
member variables, followed by the child constructor
■ Below is a demonstration program showing initialization order with inheritance. Its output is:
1a
- Parent's static class variable pstatic initialized.
1b
- Parent's static initialization block ran.
2a
- Child's static initialization block ran.
2b
- Child's static class variable cstatic initialized.
3 - Child's main(..) method ran. Invoking
Child with new.
4 - Parent's non-static class variable p initialized.
5 - Parent constructor ran.
6 - Child's non-static class variable c
initialized.
7 - Child constructor ran.
8 - The rest of Child's main(..) was run.
class
Parent {
String
p = verify("4 - Parent's
non-static class variable p initialized.");
static
String pstatic = verify("1a - Parent's static class variable pstatic
initialized.");
static
{System.out.println("1b - Parent's static initialization block
ran.");}
static
String verify(String s) {System.out.println(s); return s;}
Parent(
) {System.out.println("5 - Parent
constructor ran."); }
}
public
class Child extends Parent {
static
{System.out.println("2a - Child's static initialization block
ran.");}
String
c = verify("6 - Child's non-static
class variable c initialized.");
static
String cstatic = verify("2b - Child's static class variable cstatic
initialized.");
Child(
) {
super(
); //providing super( ) won't change
anything here
System.out.println
("7 - Child constructor
ran."); }
public
static void main(String[ ] args) {
System.out.println("3 - Child's main(..) method ran. Invoking
Child with new.");
Child
C = new Child( );
System.out.println("8 - The rest of main(..) was run."); }
}
Here is an explanation of the demonstration program above. Numbers correspond to the numbers in the output above.
To start, the class Child (see above) is run. But Child extends Parent, so Parent will be loaded first and then Child will be loaded.
1a, 1b - During Parent loading, all static items in the Parent class are run or initialized, in their source code order. This includes both static variables and static initialization blocks.
2a, 2b - Next, during Child loading, the static items in the Child class are run or initialized, again in their source code order. (Note that Child uses a different order, but the source code order is still followed.)
3 - The main(..) method in Child is run. It creates a new instance of Child. But, again, Child extends Parent, so Parent will get created first.
4 - During Parent instantiation, the Parent class member variables are set to their defaults or initialized.
5 - The Parent class constructor is called.
6 - Then Child is instantiated and all Child class member variables are initialized.
7 - Finally the Child class constructor is run. Note that explicitly calling or not calling super( ) in the Child constructor does not alter the order of steps 5 - 6 - 7.
8 - Finally, execution of Child's main(..)
method continues.