Summary of sequence problems in initialization in Java

Source: Internet
Author: User

Part of the following is a summary of their own, and part of it is to draw on other people's summary.

I. Basic concepts:

1. Construction method :

Construction methods are used to perform instance initialization of a class, which is also called a constructor.  When an object is created, the appropriate construction method is automatically called according to the list of arguments passed in. Each class has a constructor method, and if the Declaration constructor method is not displayed, the compiler automatically generates a default parameterless construction method. The default constructor method instantiates the object and assigns the Unassigned field to the default value.

NOTE: If you declare a construction method with parameters, the compiler does not automatically generate a default parameterless constructor, and if you need a constructor with no arguments, you need to display the declaration.

2. Initialize the code block :

(1) static initialization code block : Static initialization code blocks are used to implement the operations required to initialize the class, statically constructed initialization code blocks for initializing static data, or for performing a specific operation that takes only one time, before the first instance is created or any static members are referenced , the static initialization code block is called automatically.  The static initialization code block of a class executes at most once in a given program. When initializing a class, before executing the static initialization code, first initialize all the static fields in the class to their default values, and then follow the code in the static initialization code block to initialize the individual static fields.

(2) instance initialization code block : An instance initialization code block is used to initialize the actions required for an instance or an object. The instance initialization code block is used to initialize the instance data.  The instance initialization code block is automatically executed before the object instance is created. When you create a class, you first initialize all instance fields in the class to their default values before executing the instance initialization code, and then perform the initialization of each instance field in turn.

Two. Sequence of initialization:

A static field / instance field can be assigned an initial value at the time of declaration, or it can be initialized in a static / Instance code block, or an initial value is assigned in a constructor method.

When the Java class initializes, the static fields are initialized first, so when you assign an initial value to the instance field, you can refer to the static fields defined in the class, including the static fields that inherit from the parent class. Conversely, it is not established.

The initialization of the field is performed in the order of position, so that the fields of the subsequent positions can be initialized using the previously initialized fields. However, if the subsequent fields are used in field initialization, a compilation error is generated.

A const field can only be assigned one time, otherwise a compilation error occurs.

Static initialization code blocks are called automatically before the first instance is created or any static members are referenced, and the instance initialization code block is automatically called when the first instance is created, and instance initialization is performed. The static initialization of a class executes at most one time in a given program, and an instance of an object is initialized every time the object is created.

Static initialization of execution order:

Before the first instance is created or any static members are referenced, if the program is running and the specified class does not have static initialization, static initialization is performed in the following order :

(1) Static initialization of the parent class that performs the initialization class (consistent with the static initialization order of the Class)

(2) All static fields in the class are initialized to their default values, or by assignment at the time of declaration.

(3) The initialization of each static field is performed by the code in the static initialization code block, in turn.

Instance Initialization properties:

When you create the first instance of a class, if the program is running and the specified class does not perform static initialization, the static initialization is performed first, and then the instance is initialized. Instance initialization is performed in the following order :

(1) Initializes an instance of the parent class of the initialization class (in the same order as the class's instance initialization).

(2) All instance fields in the class are initialized to their default values, or by assignment at the time of declaration.

(3) The code in the code block is initialized by instance, and then the corresponding initialization is performed.

(4) in turn, the corresponding instance constructs the code block in the method, performs the corresponding initialization.

Three. Some exceptions:

1. Initialization order of static variables and instance variables: (Static variables, static initialization blocks) > (variables, initialization blocks) > constructors

Cases:

public class initialordertest{
public static String staticfield= "static variable";
Public String field= "variable";
static{
System.out.println (Staticfield);
SYSTEM.OUT.PRINTLN ("Static initialization Block");
}
{
System.out.println (field);
System.out.println ("initial block");
}
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:
static variables
Static initialization blocks
Variable
Initialize block
Constructors

2. Initialization order for inheritance: The initialization of subclasses is not done until the parent class is fully initialized, and the initialization of the static and static initialization blocks of the subclass is done before the variables of the parent class, initialization blocks, and constructors are initialized.

Class parent{
public static String p_staticfield= "Parent class-static variable";
Public String p_field= "parent class-variable";
static{
System.out.println (P_staticfield);
System.out.println ("Parent class-static initialization block");
}
{
System.out.println (P_field);
System.out.println ("Parent class-Initialization block");
}
Public Parent () {
System.out.println ("Parent class-constructor");
}
}

public class subclass extends parent{
public static String s_staticfield= "sub-class-Static variable";
Public String s_field= "subclass--variable";
static{
System.out.println (S_staticfield);
System.out.println ("Subclass-static initialization block");
}

{

System.out.println ("Subclass-Initialization block");

}

Public Subclass () {

System.out.println ("Subclass--constructor");

}
Public static void Main (string[] args) {

New Subclass ();

}

}

Operation Result:

Parent class--Static variable

Parent class--static initialization block

Subclass--Static variables

Subclass--Static initialization block

Parent class--variable

Parent class--initialization block

Parent class--constructor

Subclass--Variables

Subclass--Initialization block

Sub-class--constructor

3. Instance variables, instance initialization blocks, static variables, static variables the order of initialization blocks are not necessarily, depending on the order in which they are in the class.
Cases:

public class testorder{
public static TestA a=new TestA ();
static{
SYSTEM.OUT.PRINTLN ("Static initialization Block");
}
public static TESTB b=new Testb ();
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");
}
}

Operation Result:

Test--a

Static initialization blocks

Test--b

Summary of sequence problems in initialization in Java

Related Article

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.