<< Chapter < Page | Chapter >> Page > |
(Note that these class variables are also declared final , causing them to behave as constants.)
A ccess the out variable without an object
Because out is a class variable, System.out returns the contents of the class variable named out (an object of the System class is not required in order to access a class variable of the System class).
In general, (ignoring the possibility of subclasses and interfaces) because out is a reference variable of type PrintStream , the returned value must either be null (no object reference) or a reference to a valid PrintStream object.
Object of the PrintStream class
When the Java Virtual Machine starts an application running, it automatically instantiates an object of the PrintStream class and connects it to the standard output device . (By default, the standard output device is typically the computer screen, but it can be redirected at theoperating system level to be some other device. The following discussion assumes that the screen is the standard output device.)
Assign object's reference to out variable
When the PrintStream object is instantiated by the virtual machine, the object's reference is assigned to the class variable of the System class named out . (Because the variable named out is final, the contents of the variable cannot be modified later.)
Reference to a PrintStream object
Therefore, the expression System.out returns a reference to the PrintStream object, which is connected to the standard output device.
Many instance methods
An object of the PrintStream class contains many instance methods. This includes numerous overloaded versions of a method named println . The signature of one of those overloaded versions of the println method follows :
public void println(Object x)
Textual representation of an object
The purpose of this overloaded version of the println method is to:
In general...
A new PrintStream object can be connected to a variety of output devices when it is instantiated. However, in the special case of the PrintStream object instantiated by the virtual machine when the program starts, whosereference is stored in the class variable named out of the System class, the purpose of the object is to provide a display path to the standard output device.
Our old friend, the toString method
To accomplish this, the code in the version of the println method shown above calls the toString method on the incoming reference. (I discussed the toString method in detail in earlier modules in this collection.) The toString method may, or may not, have been overridden in the definition of the class from which the object wasinstantiated, or in some superclass of the class from which the object was instantiated.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?