The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
Covers a lot of Java OOP technology
This is not because there is a great demand for the use of this statement in real-world problems. (In fact, in a GUI-driven software product world, there is probably very little demand for the use of this statement.) Rather, it is because a lot of Java object-oriented technology is embodied in this single statement.
In that scenario, I would expect to receive a verbal dissertation of fifteen to twenty minutes in length to cover all the important points.
The short version
Let me give you the short version. There is a class named System. The System class declares three static (class) variables having the following types, names, and modifiers:
- public static final PrintStream out
- public static final InputStream in
- public static final PrintStream err
(Note that these class variables are also declared final, causing them to behave as constants.)Access 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 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 the operating 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:
- Create a textual representation of the object referred to by the incoming parameter of type Object (because Object is a totally generic type, this version of the println method can accept an incoming parameter that is a reference to any type of object)
- Send that textual representation to the output device
In general ...Our old friend, the toString method
(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, whose reference 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.)
To accomplish this, the code in the println method invokes the toString method on the incoming reference.
(I discussed the toString method in detail in earlier lessons in this miniseries.)The toString method may, or may not, have been overridden in the definition of the class from which the object was instantiated, or in some superclass of the class from which the object was instantiated.
Default version of toString
If not overridden, the default version of the toString method defined in the Object class is used to produce a textual representation of the object. As we learned in an earlier lesson, that textual representation looks something like the following:
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
