On the initialization sequence of Java variables _java

Source: Internet
Author: User

Rule 1 (without inheritance):
For static variables, static initialization blocks, variables, initialization blocks, constructors, their initialization order is
(static variable, static initialization block) > (variable, initialization block) > constructor
Proof Code:

Copy Code code as follows:

public class Initialordertest {
static variables
public static String Staticfield = "static variable";
Variable
Public String field = "Variable";
Static initialization block
static {
System.out.println (Staticfield);
SYSTEM.OUT.PRINTLN ("Static initialization Block");
}
Initializing blocks
{
System.out.println (field);
System.out.println ("initialization block");
}
Construction device
Public Initialordertest () {
System.out.println ("constructor");
}
public static void Main (string[] args) {
New Initialordertest ();
}
}

The results show:
static variables
Static initialization block
Variable
Initializing blocks
Construction device

Rule 2 (with inheritance):
The initialization of static and static initialization blocks of subclasses is done before the variables, initialization blocks, and constructors of the parent class are initialized

Copy Code code as follows:

Class Parent {
static variables
public static String P_staticfield = "Parent class-static variable";
Variable
Public String P_field = "Parent class-variable";
Static initialization block
static {
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
Initializing blocks
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}
Construction device
Public Parent () {
System.out.println ("Parent class-constructor");
}
}//If you want to put these two classes in the same file and the name is subclass, the parent cannot add public before
public class Subclass extends Parent {
static variables
public static String S_staticfield = "Subclass--static variable";
Variable
Public String S_field = "Subclass--variable";
Static initialization block
static {
System.out.println (S_staticfield);
System.out.println ("Subclass--static initialization block");
}
Initializing blocks
{
System.out.println (S_field);
System.out.println ("subclass--initialization block");
}
Construction device
Public Subclass () {
System.out.println ("Subclass--constructor");
}
Program entry
public static void Main (string[] args) {
New Subclass ();
}
}

The results show:
Parent class--Static variables
Parent class--static initialization block
Subclasses--Static variables
Subclass--Static initialization block
Parent class--variable
Parent class--Initialize block
Parent class--builder
Subclasses--variables
Subclass--Initialize block
Subclass--Builder

Rule 2 (Static variables and static initialization blocks):
Static variables and static initialization blocks are initialized according to the order in which they are defined in the class. Similarly, variables and initial
The initiation block also follows this rule.

Copy Code code as follows:

public class Testorder {
static variables
public static Testa a = new Testa ();
Public TESTC C = new TESTC ();
static variables
public static Testb B = new Testb ();
Public Testorder () {
System.out.println ("in Constract");
}
Static initialization block
static {
SYSTEM.OUT.PRINTLN ("Static initialization Block");
}
public static void Main (string[] args) {
New Testorder ();
}
}
Class Testa {
Public Testa () {
System.out.println ("Test--a");
}
}
Class Testb {
Public Testb () {
System.out.println ("Test--b");
}
}
Class TESTC {
Public Testc () {
System.out.println ("Test--c");
}
}

The results show:
Test--a
Test--b
Static initialization block
Test--c
In Constract

Personal Summary:
First, after the father, after the son, from top to bottom, the first variable after the construction

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.