The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
(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:
ClassName@HexHashCode
Overridden version of toString method
If the class from which the object was instantiated (or some superclass of that class) contains an overridden version of the toString method, runtime polymorphism kicks in and the overridden version of the method is executed to produce the textual representation of the object.
The Date class overrides toString
In the case of this sample program, the object was instantiated from the Date class. The Date class does override the toString method.
When the overridden toString method is invoked on a Date object's reference, the String returned by the method looks something like that shown in Figure 6.
Mon Sep 17 09:52:27 CDT 2001 Figure 6
You will recall that this is the output that was produced by the code shown in Listing 8 and Listing 12.
More than you ever wanted to know ...
And that is probably more than you ever wanted to know about the expression System.out.println....
It is also probably more than you ever wanted to know about class variables, class methods, instance variables, and instance methods.
Some cautions
Before leaving this topic, I do want to express some cautions. Basically, I want to suggest that you use static members very sparingly, if at all. Several good sets of guidelines have been published regarding the use of static members. As of this writing, you can find one of them, written by Mark L. Fussell, at the following URL.
Guidelines for use of static members
I will try to summarize the general sense of most published guidelines in a few words.
Static variables
To begin with, don't ever use static variables without declaring them final unless you understand exactly why you are declaring them static.
(Static final variables, or constants, are often very appropriate. See the fields in the Color class for example.)I can only think of a few situations where the use of a non-final static variable might be appropriate.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系: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
