To begin with, we will produce a class declaration that is just able to represent values that are dates: it provides no operations, and so there will be little that we can do with these date values.
Within each object of this class, three ints will be used to represent the year, month and day parts of a date. Here is the class declaration:
0237: // A class for representing values that are dates. // Date.java 0238: public class Date 0239: { 0240: public int year; 0241: public int month; 0242: public int day; 0243: }This class declaration for the class Date needs to be stored in the file Date.java.
Here is a program that uses the class Date. It is called NoelProg, and so these lines need to be stored in the file NoelProg.java:
0244: // This program creates an object of the class Date // NoelProg.java 0245: // and then sets its fields to represent Christmas Day 1999. 0246: public class NoelProg 0247: { 0248: public static void main(final String[] pArgs) 0249: { 0250: final Date tNoelDate = new Date(); 0251: tNoelDate.year = 1999; 0252: tNoelDate.month = 12; 0253: tNoelDate.day = 25; 0254: System.out.println(tNoelDate.year + "-" + 0255: tNoelDate.month + "-" + tNoelDate.day); 0256: } 0257: }
When we want to execute the NoelProg program, we first have to compile the two pieces of Java source code:
javac Date.java javac NoelProg.javaThis produces the files Date.class and NoelProg.class. Since it is the file NoelProg.java that contains the main method, we can execute the program by typing:
java NoelProg
What does the NoelProg program do? The first statement:
final Date tNoelDate = new Date();is a declaration. The left-hand side establishes a reference variable called tNoelDate. The initializer on the right-hand side is a class instance creation expression:
new Date()This creates an object that is just big enough to hold the fields of the class, i.e., the three fields called year, month and day.
Each field will be initialized to a value which depends on the type of the field. The default values for fields were given earlier. As the three fields of the class Date have the type int, they will be initialized to zero. The initializer causes tNoelDate to be assigned a value that points to this object.
Following the declaration of tNoelDate, there are three assignment statements that assign values to each of these three fields. For example:
tNoelDate.year = 1999;puts a value in the year field of tNoelDate. Here the dot notation introduced earlier is being used. Although it is possible to assign values to the year, month and day fields that do not represent a date, we will ignore this deficiency.
The last statement of the program outputs the line:
1999-12-25
Here is some jargon: a piece of code that uses another class is said to be a client of the class. So the program NoelProg is a client of the class Date.
Although this is exciting because we have declared this class ourselves, there is nothing new about the way in which we are using the class. It is much like what we did with the class Point earlier.