java object initialization problem--Ali

1. Why introduce

Because the java object initialization problem is a relatively basic java knowledge point, and it is also a question that is asked repeatedly in many interviews. Therefore, we will understand him through the example and analysis of Ali engineer.

2. Cause problems

It will trigger the Java object initialization order problem, which is relatively rare.

3. Examples

package com.xn.web.budget;

public class Test2 extends Test {
    public int a = 100;

    public Test2() {
        super();
        System.out.println(a);
        a = 200;
    }

    public static void main(String[] args){
        System.out.println(new Test2().a);
    }
}

package com.xn.web.budget;

public class Test {
    public Test() {
        System.out.println(((Test2)this).a);
    }
}

Result output:

0
100
200

Parse the initialization sequence of objects:
1. Allocate memory space for class A, and initialize all member variables to default values, including primitive types (int=0, boolean=false,...) and Reference types.
2. Call the class A constructor.
3. Call the class B constructor.
4. Call the Object empty constructor. (The java compiler will add this constructor by default, and the object constructor is an empty function, so it returns immediately)
5. Initialize class B member variables, because class B has no member variables, skip it.
6. Execute sysout to output the member variable small a of subclass A. // This time is 0
7. Initialize the class A member variable, and assign the value of 100 to the small a of the class A member variable.
8. Execute sysout to output the member variable small a of the current class A. // At this time, it is 100
9. Assign the member variable of the current class A to 200.
10. Execute sysout in the main function, and output the member variable small a of the class A instance. // this time is 200

The two lines in bold are the key points. The conclusion is that member variables are initialized after the parent class constructor is called. Before that, the values ​​of member variables are default values .

In fact, this kind of problem is familiar with the principle on the one hand. In essence, as long as too much business logic is not inserted into the constructor, the probability of problems will be much lower.

Finally, let's take a look at the Java class object initialization sequence definition given in JLS, which is a process description with conditional branches:

  1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
  2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
  3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
  4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
  5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

Quote from: Teleport

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325944995&siteId=291194637