小编Bhu*_*mar的帖子

为什么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();
    }

}
Run Code Online (Sandbox Code Playgroud)

java

2
推荐指数
2
解决办法
93
查看次数

线程"main"中的异常:java.lang.StackOverflowError,为什么?

这是我的代码; 导致StackOverflow错误:

public class NucleousInterviewQuestion {

NucleousInterviewQuestion interviewQuestion = new  NucleousInterviewQuestion();

public NucleousInterviewQuestion() {
    // TODO Auto-generated constructor stub
}

public static void main(String[] args) {
    NucleousInterviewQuestion interviewQuestion= new NucleousInterviewQuestion();
 }
}
Run Code Online (Sandbox Code Playgroud)

java

1
推荐指数
1
解决办法
2212
查看次数

标签 统计

java ×2