在下面的代码中,为什么该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) 这是我的代码; 导致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 ×2