简体   繁体   English

当一个类的实例本身被创建时,为什么构造函数中的语句没有被执行?

[英]When an instance of a class is created in itself, then why the statements in the constructor are not executed?

When a class is instantiated, its constructor is called.当一个类被实例化时,它的构造函数被调用。 In this example, I want to check when a StackOverflow error occurred.在这个例子中,我想检查何时发生 StackOverflow 错误。 But the statements declared inside the constructor are not executed, why?但是构造函数里面声明的语句没有执行,为什么呢? see the following code看下面的代码

public class StackOverFlowSampleMain {
    StackOverFlowSampleMain oomeSampleMain = new StackOverFlowSampleMain();
    static int x = 0;

    StackOverFlowSampleMain() {
        x++; //aren't these lines supposed to be executed?
        System.out.println(x);
    }

    public static void main(String[] args) {
        StackOverFlowSampleMain oomeSampleMain = new StackOverFlowSampleMain();

    }
}

Member initialization happens before the constructor's body.成员初始化发生在构造函数的主体之前。 So when you create a StackOverFlowSampleMain instance, the first thing it does is to initialize its oomeSampleMain member.因此,当您创建StackOverFlowSampleMain实例时,它所做的第一件事就是初始化其oomeSampleMain成员。 It, in turn, attempts to initialize its own oomeSampleMain member, and so on, until the program crashes with a StackOverflowError , so the incrementing of x is just never reached.反过来,它会尝试初始化自己的oomeSampleMain成员,依此类推,直到程序因StackOverflowError崩溃,因此从未达到x的递增。

If you want to measure when the StackOverflowError occurs, you could move the code that causes it to the end of the constructor:如果要测量StackOverflowError何时发生,可以将导致它的代码移动到构造函数的末尾:

public class StackOverFlowSampleMain {
    StackOverFlowSampleMain oomeSampleMain;
    static int x = 0;

    StackOverFlowSampleMain() {
        x++;
        System.out.println(x);
        oomeSampleMain = new StackOverFlowSampleMain(); // Here, after x is printed
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么类实例本身没有公共构造函数? - Why class instance itself has no public constructor? 当使用静态引用创建对象时,为什么在静态块之前执行实例块和默认构造函数? - When objects created with static reference, why do instance block & default constructor get executed before static block? 为什么我无法访问在构造函数中创建的 class 实例 ( Java ) - Why can I not access an instance of class that is created in a constructor ( Java ) 如果未在main方法中创建类的实例,是否会调用默认构造函数? - When an instance of a class is not created in the main method, will the default constructor be called? 为什么要让一个类创建自己的实例? - Why make a class create an instance of itself? 为什么类返回自身的实例? - Why does a class return an instance of itself? 传递构造函数参数时未创建Yaml实例 - Yaml instance not created when constructor argument is passed 为什么执行构造函数中的calculate()? - Why is calculate() in the constructor executed? 是否可以创建类的实例进行测试而无需调用构造函数? - Can an instance of a class be created for testing without calling the constructor? 当我将 Enum 实例变量设置为作为选项包含在 Enum 类本身中的值时,它无法解析为类型。为什么? - When I set an Enum instance variable as a value that is included as an option in the Enum class itself, it can not be resolved to a type.Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM