Java Reference
In-Depth Information
The second “Some Text” line in the output window is the result of your
new println statement. This statement printed the text in data member
msgText directly from the object myErrorMsg . It can do this because msgText
has been defined as a public data member in the class ErrorMsg . For the
sake of brevity, the entire output window is not shown here.
5. Now you will try to access ErrorMsg 's other data members from Hello-
World. Add the bolded lines of code after your new println statement so
that it looks like this:
// Set the text item in ErrorMsg to some text String, and print its
// contents:
myErrorMsg.setErrorMsg ("Some Text");
tempMsg = myErrorMsg.getErrorMsg ();
System.out.println (tempMsg);
// Print the contents of ErrorMsg's String data member directly.
System.out.println (myErrorMsg.msgText);
// Try to access the other data member (s) in ErrorMsg.
System.out.println ("msgSize " + myErrorMsg.msgSize);
System.out.println ("counter " + myErrorMsg.counter);
6. What happens when you try to compile this class (remember to save the file
as a text file)?
javac HelloWorld.java
The compiler knows that private data members cannot be accessed, so
don't try it again!
7. Remove the offending statement, and recompile. Now try running the
modified program:
javac HelloWorld.java
java HelloWorld
Your output window should look like this:
Hello \World!
Some Text
Some Text
msgSize 9
Some New Text
Some Text for #2
Some New Text
SOME NEW TEXT
Search WWH ::




Custom Search