Java Reference
In-Depth Information
System.out.println(month + “/” + day + “/” + year);
}
}
The Date class has three fields, all of type int: day, month, and year. The Date
class also has two methods: getDay() and printDate(). The getDay() method
declares that it returns an int, and notice within getDay() it returns the day
field, which is an int. The printDate() method is declared void and does not
return a value. Both methods have an empty parameter list, which is denoted
by the empty parentheses.
In the following DateProgram, a Date object is instantiated and the two
methods in the Date class are invoked. Study the DateProgram carefully and
try to determine its output.
public class DateProgram
{
public static void main(String [] args)
{
System.out.println(“Within main...”);
Date today = new Date();
today.day = 25;
today.month = 12;
today.year = 2003;
System.out.println(“The day is “ + today.getDay());
System.out.println(“Printing the date...”);
today.printDate();
System.out.println(“What is displayed next?”);
today.getDay();
}
}
The first line of output of the DateProgram is as follows because it is the first
statement in main():
Within main...
A Date object is then instantiated, and its fields are initialized to represent
December 25, 2003.
Another System.out.println() statement occurs, and the string “The day is “
is concatenated with today.getDay(). The getDay() method is invoked on the
today object, causing flow of control to jump to the getDay() method.
Notice that the string “The day is “ is not displayed yet. The call to
System.out.println() has not occurred yet because order of operations
requires that the method call to getDay() occur before the call to println().
After getDay() returns a value, the string concatenation is evaluated, and
then println() will be invoked.
Search WWH ::




Custom Search