Initialization Order of Java classes

Source: Internet
Author: User

For static variables, static initialization blocks, variables, initialization blocks, constructors, theirinitialization order is (static variable, static initialization block) > (variable, initialization block) > constructor。 We can also verify this with the following test code:

public class Initialordertest {
static variables
Public static String Staticfield = "static variable";
Variable
Public String field = "Variable";
Static initialization blocks
static {
System.out.println (Staticfield);
SYSTEM.OUT.PRINTLN ("Static initialization Block");
}

Initialize block
{
System.out.println (field);
System.out.println ("initialization block");
}

Constructors
Public Initialordertest () {
System.out.println ("constructor");
}
public static void Main (string[] args) {
New Initialordertest ();
}
}
Running the above code, we will get the following output result:
1. Static variables
2. Static initialization block
3. Variables
4. Initializing blocks
5. Constructors
This is in full conformity with the above mentioned. So what happens to the inheritance? We still get the final result with a test code:
Class Parent {
static variables
public static String P_staticfield = "Parent class-static variable";
Variable
Public String P_field = "Parent class-variable";
protected int i = 9;
protected int j = 0;
Static initialization blocks
static {
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
Initialize block
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}//Constructors
Public Parent () {
System.out.println ("Parent class-constructor");
System.out.println ("i=" + i + ", j=" + j);
J =;

}
}
public class Subclass extends Parent {
static variables
public static String S_staticfield = "sub-class-Static variable";
Variable
Public String S_field = "Subclass--variable";
Static initialization blocks
static {
System.out.println (S_staticfield);
System.out.println ("Subclass-static initialization block");
}
Initialize block
{
System.out.println (S_field);
System.out.println ("Subclass-Initialization block");
}//Constructors
Public Subclass () {
System.out.println ("Subclass--constructor");
System.out.println ("i=" + i + ", j=" + j);
}//Program Entry
public static void Main (string[] args) {
System.out.println ("Subclass Main Method");
New Subclass ();
}
}
Run the above code, and the results will immediately appear in front of us:
Parent Class--Static variable
Parent class--static initialization block
Subclass--Static variables
Subclass--Static initialization block
Sub-class Main method
Parent class--variable
Parent class--initialization block
Parent class--constructor
I=9, j=0
Subclass--Variables
Subclass--Initialization block
Sub-class--constructor
i=9,j=20Now, the results have been self-evident. The initialization of a static variable and a static initialization block for a subclass is done before the parent class's variables, initialization blocks, and constructors are initialized.static variables, static initialization blocks, variables, initialization blocks are initialized in order, depending on the order in which they appear in the class.

Perform process analysis

(1) Access Subclass.main (), (this is a static method), so the loader will look for the compiled subclass class code (that is, the Subclass.class file). During loading, the loader notices that it has a base class (that is, what extends means), and then it loads the base class. Whether you create a base class object or not, this process always happens. If the base class also has a base class, the second base class is also loaded, and so on.

(2) performs static initialization of the base class, then static initialization of the next derived class, and so on. This order is important because the static initialization of a derived class is likely to depend on the correct initialization of the base class member.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Initialization Order of Java classes

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.