-4

How can I print date using constructor chaining in java ? I am getting error after this(6,2018) stating I need two argument constructor why is that? And when i use the quick fix a new constructor Date(int i, int j) is created. I am unable to understand this too. Here's what I tried -:

public class Date
{   
    int dd;
    public Date()
    {
        this(30);
    }

    public Date(int dd)
    {
        this(6,2018);
        this.dd=dd;
    }

    public static void main(String arg[])
    {
        Date ob= new Date();
    }

}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
naman_rai
  • 3
  • 1
  • 2
    Aside from the fact that you are missing a constructor, what you are trying to do is a horrible idea. The design is too rigid. It would be better to model days, monthis and years as separate objects and then use composition to model a date object. Or use [the java time API](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html), because [dates are a pain in the a**](https://www.youtube.com/watch?v=-5wpm-gesOY). – Turing85 Jun 29 '18 at 20:16
  • Please explain what you are trying to obtain? Restrictions? Homeowrk? Are you allowed to use standard Java date and time classes like ``java.time``API? – Ole V.V. Jun 29 '18 at 21:12

1 Answers1

1

you are attempting to call a constructor with two parameters: this(6,2018); 6 and 2018 which you do not seem to have created.

You need to create a constructor Date(int x, int y){}

John Kane
  • 4,383
  • 1
  • 24
  • 42