我知道這是錯誤的,但我想知道爲什麼我收到獲取stackOverflowError異常的原因是什麼?
class Student{
String student;
int rollno;
Student stud=new Student("s",20);//(1st object)Thrown exception because of this line
Student(String student,int rollno){
this.student=student;
this.rollno=rollno;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stud=new Student("R",101);
}
}
Exception in thread "main" java.lang.StackOverflowError
at sai.Student.<init>(Student.java:8)
at sai.Student.<init>(Student.java:8).........
的原因。當我創建了一個對象(即第一個對象),我不會得到這個錯誤,但是當我創建了兩個對象,我得到這個錯誤。
您沒有創建兩個對象。您正在嘗試創建無限數量的對象。 – Eran
你能解釋一下如何創建無限對象嗎? – Swati