Java Reference
In-Depth Information
would produce
Hello World!
null
This
println
statement causes the text
null
to appear in the output window.
On the other hand, the version of
ErrorMsg
in the exercises initialized the
msgText
data member to one space. Therefore, these statements
System.out.println ("HelloWorld!");
tempMsg = myErrorMsg.getErrorMsg ();
System.out.println (tempMsg);
Hello World!
cause a single space character to appear in the output window (which is invisible).
The HelloWorld application then stores some text, in this instance, of
ErrorMsg
.
HelloWorld uses the
setErrorMsg
method to store this text.
myErrorMsg.setErrorMsg ("Some Text");
HelloWorld next gets the text item from
ErrorMsg
and prints out the contents of
this data member. Note that the output for this statement is the text “Some Text.”
tempMsg = myErrorMsg.getErrorMsg ();
System.out.println (tempMsg);
Some Text
Next, HelloWorld modifies the data member and prints out its new contents.
In this case, the output for this print statement is the text “Some New Text.”
myErrorMsg.setErrorMsg ("Some New Text");
tempMsg = myErrorMsg.getErrorMsg ();
System.out.println (tempMsg);
Some New Text
Finally, you modified HelloWorld to create a second instance of
ErrorMsg
and
stored a reference to this new instance in the variable
myErrorMsg2
. You then stored
the
String
“Some Text for #2” in its data member, using the
setErrorMsg
method: