
The API documentation, which should be your first reference for understanding a system library class, explains what to pass in for the parameters. Also of note are the odd-looking parameter values being passed to the Date constructor.
#Constructor overloading in java code#
This code uses one of the deprecated constructors for Date. You will need an import statement at the top of CourseSessionTest: New CourseSession("ABCD", "200", startDate) ĭate sixteenWeeksOut = new Date(year, month, date) ĪssertEquals(sixteenWeeksOut, session.getEndDate()) Here's the test to be added to CourseSessionTest.ĭate startDate = new Date(year, month, date) With this information, you decide to design the CourseSession class so that users of the class need only supply the start date-your class will calculate the end date. You could provide both start and end dates to a CourseSession constructor, but you have been told that sessions are always 16 weeks (15 weeks of class, with a one-week break after week 7). In the student information system, course sessions need to have a start and end date, marking the first and last days of class. As the exercise progresses, you will eliminate the warnings using an improved solution. There's always a better way that does not generate a warning. Warnings are for the most part bad-they indicate that you're doing something in code that you probably should not be. You'll see that their use generates warnings from the compiler. In this exercise, you will actually use the deprecated methods. If you browse the Date class in the API documentation, you will see that Sun has clearly marked the deprecated methods and constructors. This means that the methods and constructors are still available for use, but the API developers are warning you that they will remove the deprecated code from the next major release of Java. Sun chose to avoid this unhappy circumstance by instead deprecating the constructors and methods in Date. However, if Sun were to have changed the Date class, large numbers of existing applications would have had to have been recoded, recompiled, retested, and redeployed. The cleanest approach would have been for Sun to simply remove the offending constructors and methods. This meant that the constructors and getters/setters in the Date class were no longer needed as of J2SE 1.1.

The Calendar class provides the ability to work with dates by their constituent parts. The intent was for the Calendar classes to supplement the Date class. The Date class was not designed to provide support for internationalized dates, however, so the designers of Java introduced the Calendar classes in J2SE 1.1.

There are also many methods that allow getting and setting of fields on the Date, such as setHour, setMinutes, getDate, and getSeconds. A final constructor, one that takes no parameters, constructs a timestamp that represents "now," the time at which the Date object was created. A fifth constructor allows you to construct a date using the number milliseconds since the epoch. A fourth constructor allows you to construct a date from an input String. In the Date class, three of the constructors allow you to specify time parts (year, month, day, hour, minute, or second) in order to build a Date object for a specific date and time.

#Constructor overloading in java how to#
You will learn how to create multiple constructors for your class in this lesson. It is possible and often desirable to provide a developer with more than one way to construct new objects of a class type. The Date class provides a handful of constructors. Agile Java¿: Crafting Code with Test-Driven Development
