有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么getX()方法首先执行而不是构造函数

在下面的代码段中,为什么getX()方法首先执行而不是构造函数:

public class ConsructorQuestion {

    int x = getX();  // same this.getX();
    public int getX() {
        System.out.println(x + "  via method  ");
        return 10;
    }

    public ConsructorQuestion() {
        System.out.println(x+"  via constructor");
    }

    public static void main(String[] args) {
        ConsructorQuestion t = new ConsructorQuestion();
    }

}

共 (2) 个答案

  1. # 1 楼答案

    如果构造函数没有链接到同一类中的另一个构造函数(通过调用^{),编译器会将字段初始值设定项(和实例初始化块)注入类中每个构造函数的开头(就在调用super(...),隐式或显式之后)。如果你看一下生成的字节码,你会发现你的类是这样的:

    public class ConsructorQuestion {
    
        int  x;                                             // ***
        public int getX(){
            System.out.println(x+"  via method  ");
            return 10;
        }
    
        public ConsructorQuestion() {
            this.x = getX();                                // ***
            System.out.println(x+"  via constructor");
        }
    
        public static void main(String[] args) {
            ConsructorQuestion t = new ConsructorQuestion();
        }
    
    }
    

    构建实例时的操作顺序包含在JLS§12.5中:

    Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

    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 (§8.8.7.1) 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.

    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.

    注意#4和#5的顺序

  2. # 2 楼答案

    实例变量在构造函数体执行之前初始化(但在超类构造函数体执行之后)

    因此,在您的示例中,int x = getX();在构造函数主体之前执行

    这相当于将x的初始化移动到构造函数的第一行:

    int x;
    public int getX(){
        System.out.println(x+"  via method  ");
        return 10;
    }
    
    public ConsructorQuestion() {
        x = getX();
        System.out.println(x+"  via constructor");
    }