Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9075907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:58:57+00:00 2026-06-16T18:58:57+00:00

Possible Duplicate: Why I’m getting StackOverflowError I am using two classes: Date and Exam.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:58:58+00:00Added an answer on June 16, 2026 at 6:58 pm

    As James said, return (this.equals(d)); is definitely wrong. Supposing your Date class has attributes for year, month and day, you should rather try something like

    public boolean equals(Object o) {
        if (! o instanceof Date) return false;
        Date d = (Date)o;
        return this.year == d.year && this.month == d.month && this.day == d.day;
    }
    

    Note it is important that the static type of the parameter is Object, see the answer of James

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Getting time and date from timestamp with php I am trying to
Possible Duplicate: Getting unix timestamp from Date() I am having the date Fri, 09
Possible Duplicate: Getting Using two-stage rotation animation warning with UIImagePickerController In my iphone app
Possible Duplicate: Getting Index of an Object from NSArray? I have one Array and
Possible Duplicate: Getting pixel data from an image using java I was already browsing
Possible Duplicate: Getting Filename from file descriptor in C Obtain filename from file pointer
Possible Duplicate: Getting the ID of the element that fired an event using JQuery
Possible Duplicate: Getting a FILE* from a std::fstream Is there a way to obtain
Possible Duplicate: Getting the timestamp from last saturday (every week)? If today is Sunday,
Possible Duplicate: Getting the minimum of two values in sql I'm trying to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.