«^»
4.5. Stage B: adding a constructor and a method declaration

4.5.1. Stage B1: adding a constructor declaration

The three assignment statements in the NoelProg program (given above) ensure that the Date object has the values that we want it to have. When we create an object, we will frequently want to assign values to all of the fields of the object. For this reason, Java allows class declarations to have constructors.

With a class to represent dates, an obvious constructor is one that creates a date object from three integers:

final Date tNoelDate = new Date(1999, 12, 25);
Here the class instance creation expression uses a constructor that has three int arguments. This is only possible if the class declaration for Date has a constructor declaration that has three int arguments:
public Date(final int pYear, final int pMonth, final int pDay)
{
   year = pYear;
   month = pMonth;
   day = pDay;
}
In many ways, a constructor looks like a method declaration. However, there are two differences: there is no result type and the declaration has the same name as the class.

So, when the declaration:

final Date tNoelDate = new Date(1999, 12, 25);
is executed, first the object is constructed with default initial values, and then the constructor is executed. So, the values 1999, 12, 25 are assigned to pYear, pMonth and pDay, and then the block of the constructor leads to the following statements being executed:
year = 1999;
month = 12;
day = 25;
A constructor can refer to the fields of the object being initialized by using the names of the fields. So these statements result in the fields of the object having their values changed. The final act of the declaration is to make tNoelDate refer to the object that has just been created by the class instance creation expression.

After this declaration, the NoelProg program executes:

tNoelDate.day++;
This statement increases the value of the day field of this object by 1.

4.5.2. Stage B2: using a method to display the value of an object

The NoelProg program that was given earlier outputs the value of a Date object by using:

System.out.println(tNoelDate.year + "-" +
                   tNoelDate.month + "-" + tNoelDate.day);
Displaying the value of an object is a common task and: it is useful to put the code for outputting a date into a method.

The following class declaration for Date includes a method declaration for a method called display:

0258: // A class for representing values that are dates.              // Date.java
0259: public class Date 
0260: {
0261:    public int year;
0262:    public int month;
0263:    public int day;
0264:    public Date(final int pYear, final int pMonth, final int pDay) 
0265:    {
0266:       year = pYear;
0267:       month = pMonth;
0268:       day = pDay;
0269:    }
0270:    public void display()
0271:    {
0272:       System.out.println(year + "-" + month/10 + month%10 +
0273:                                 "-" + day/10 + day%10);
0274:    }
0275: }
Its use is illustrated by this version of the NoelProg program:
0276: // This program creates an object of class Date             // NoelProg.java
0277: // representing Christmas Day 1999, then moves 
0278: // the day field on by 1, and then outputs the new date.
0279: public class NoelProg
0280: {
0281:    public static void main(final String[] pArgs)
0282:    {
0283:       final Date tNoelDate = new Date(1999, 12, 25);
0284:       tNoelDate.day++;
0285:       tNoelDate.display();
0286:    }
0287: }
When the NoelProg program executes the statement:
tNoelDate.display();
the method called display will get called, and it will be applied to the object pointed to by the tNoelDate variable. When the block of display is executed, i.e., when the statement:
System.out.println(year + "-" + month/10 + month%10 + "-" + day/10 + day%10);
is executed, the references to year, month and day are references to the year, month and day fields of tNoelDate. The uses of /10 and %10 ensure that two digits are always output for the month and day values.

The call of this method will output the line:

1999-12-26