Possible Duplicate:
Why I’m getting StackOverflowError
I am using two classes: Date and Exam. Date sets a Date object from three integers: day, month,year; Exam sets an Exam object from one String courseName and one Date Object.
I am trying to run this code:
public Exam(String name, Date d)
{
courseName=name;
examDate=new Date(d);
}
//**a method that checks if two dates are equal**
public boolean equals (Date r)
{
return (examDate.equals(r));
}
public static void main(String[] args)
{
Date d=new Date(11,11,2011);
String a=new String("OOP");
Exam b=new Exam(a,d);
Date c=new Date(11,11,2011);
System.out.println(b.equals(c));
}
when I try to run the code I get the error Exception in thread “main” java.lang.StackOverflowError
The error says that the problem is on a line in Date class, which checks if two dates are equal:
public boolean equals (Date d)
{
return (this.equals(d));
}
I’ll be thankful to know why that happens.
As James said,
return (this.equals(d));is definitely wrong. Supposing yourDateclass has attributes for year, month and day, you should rather try something likeNote it is important that the static type of the parameter is
Object, see the answer of James