Java Reference
In-Depth Information
The statement import java.util at the beginning of the class tells the compiler
to introduce the definitions for the java.util classes into the Diagnostic class.
You needed to do this to use the Java Date class in the code.
You introduced a data member named msgSize in the class PrintfileErrorMsg .
This variable hid the similarly named variable in the class TextMessage from
HelloWorld, that is, when HelloWorld performed the following statement:
// Print this data member in PrintfileErrorMsg.
System.out.println ("msgSize for PrintfileErrorMsg = " +
myErrorMsg3.msgSize);
Before the change, the variable in TextMessage was printed. After the change,
the variable in PrintfileErrorMsg was printed.
You constructed an interface definition ( WriteLineInterface ) and defined a
supporting class ( WriteLine ). You then used this class to simplify the logic in
PrintfileErrorMsg . In a real system, any other class could also use the Write-
Line infrastructure in order to centrally manage printing functions.
You demonstrated how polymorphism can be used. You sent the same message
( setErrorMsg (String) ) to instances of three different classes ( ErrorMsg , Print-
fileErrorMsg , and Diagnostic ). Each of these classes respond to the message in
a unique way:
ErrorMsg simply stored the String and did not print any text.
PrintfileErrorMsg printed the message after skipping some lines and
centering the String in the print line. PrintfileErrorMsg also printed the
values in the two msgSize variables.
Diagnostic printed the message and then performed a standard Java
function to print the current call stack. This function would be very use-
ful in a production system for recording contextual information appro-
priate for “postmortem” analysis of application or system failures.
You used a new object in the System class named err . This object is similar to
the System.out object you have been using all along, except it is designed to
write error messages instead of standard, or informative messages. Both objects
will write to the display device by default.
Search WWH ::




Custom Search