0

I prepared Date and DateTest classes which show below;

Date Class

public Date( String m, int d, int y)
{
this("m",d,y);
}

DateTest Class

Date myDate2 = new Date("april",18 , 2013);
System.out.println(myDate2);

But I receive below error message. Can anyone advise me what the problem is and how I can fix it.

Error Message Exception in thread "main" java.lang.Error: Unresolved compilation problem: Recursive constructor invocation Date(String, int, int)

at Date.<init>(Date.java:24)
at DateTest.main(DateTest.java:10)

2 Answers 2

4

Judging by the error message, I'd say that calling the this(), is what's causing the problem

public Date( String m, int d, int y)
{
this("m",d,y);
}

apparently calling this("m",d,y); in your constructor means that your'e making recursive calls in your constructor, which doesn't even make sense.


My guess is that maybe you're looking to do something along the lines of

public Date( String m, int d, int y)
{
    this.Month = m;
    this.Day = d;
    this.Year = y;
}
Sign up to request clarification or add additional context in comments.

2 Comments

so, how can I fix it Sam ?
@Behzat get rid of that line. If you elaborate on what you think that line is supposed to to, I might be able to tell you how to replace it
1

Calling this inside the constructor is making it infinitely recursive. Hence that error. You would be better off creating a constructor with some setter methods. Read about constructor design pattern, that should help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.